Please help me with this C programming. it needs to be in C. thank you Write a p
ID: 3804072 • Letter: P
Question
Please help me with this C programming. it needs to be in C. thank you
Write a program that builds a doubly linked linked-list and
populate the linked list with the integers 0 through N.
Note that you must trap cases where you have no command line
arguments and where you have too many command line arguments.
It is OK in both of these cases to print an error message and
exit the program.
Your program must display the value of N (formatted as shown
below - tab seperated) followed by a blank line. You may
also want to "man 3 atoi()".
As you populate the list, print out the values being inserted
(formatted as shown below - tab seperated), followed by a
blank line.
Once the list is populated, print out the contents from left
to right, then print out a blank line, then print out the
contents from right to left.
The inputs (data or payload) will simply come from a for loop
(0 to N), in that order! Here is what your output MUST look
like (except N will vary).
Command line argument: 3
Input data: 0
Input data: 1
Input data: 2
Input data: 3
Left to right output: 0
Left to right output: 1
Left to right output: 2
Left to right output: 3
Right to left output: 3
Right to left output: 2
Right to left output: 1
Right to left output: 0
Finally, pictorally, the linked list should look like this:
Head -> |---| -> |---| -> NULL
| 0 | | 1 |
NULL <- |___| <- |___| <- Tail
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
struct node *prev;
};
//this link always point to first Link
struct node *head = NULL;
//this link always point to last Link
struct node *last = NULL;
struct node *current = NULL;
int length() {
int length = 0;
struct node *temp;
temp = head;
while( temp != NULL) {
temp = temp->next;
length++;
}
return length;
}
//display the list in from left to right
void displayL2R() {
struct node *temp = head;
while(temp != NULL) {
printf(" Left to right output: %d ",temp->data);
temp = temp->next;
}
}
//display the list from right to left
void displayR2L() {
//start from the last
struct node *temp = last;
while(temp != NULL) {
printf(" Right to Left output: %d ",temp->data);
temp = temp ->prev;
}
}
//insert link at the last location
void insert(int data) {
struct node *dataNode = (struct node*) malloc(sizeof(struct node));
dataNode->data = data;
dataNode->next = NULL;
dataNode->prev = NULL;
if(head == NULL) {
last = dataNode;
head = dataNode;
} else {
//make link a new last link
last->next = dataNode;
//mark old last node as prev of new link
dataNode->prev = last;
}
//point last to new last node
last = dataNode;
}
int main(int argc, char *argv[] ) {
if( argc >= 2 ) {
int max = atoi(argv[1]);
int i = 0;
for(i = 0; i <=max; i++)
insert(i);
displayL2R();
printf(" ");
displayR2L();
}
else {
printf("One argument expected. ");
}
return 0;
}
---output---
C:UsersRavi>D: aviChegg-cpp-current.exe 4
Left to right output: 0
Left to right output: 1
Left to right output: 2
Left to right output: 3
Left to right output: 4
Right to Left output: 4
Right to Left output: 3
Right to Left output: 2
Right to Left output: 1
Right to Left output: 0
C:UsersRavi>