Question: Need help with continuation for this code. A dd the command and function to reverse the list . The reverse function has the following constraints:
Need help with continuation for this code.
Add the command and function to reverse the list. The reverse function has the following constraints:
Must be recursive. Cannot make a copy of the list to create a duplicate or external input list. (A copy of an important pointer is acceptable.)
Accept the command "r" to invoke the reverse function. This is all about pointer manipulation.
The success of the reverse command can be verified by using the "p" or print command from code below.
#include
#include
typedef struct node { int data; struct node *next; } node;
struct node * addToList(struct node *base, int data) { struct node *newNode = NULL;
if (base == NULL) { base = malloc( sizeof(struct node)); // malloc defend base->data = data; base->next = NULL; return base; } //Find the bottom of the list starting from the base while (base->next != NULL) { base = base->next; } //Now at the end of the list newNode = malloc( sizeof(struct node)); //get memory for new node //Defend against bad malloc base->next = newNode; // add the new node to bottom of the list newNode->data = data; // slap the data into the list newNode->next = NULL; // terminate the list by setting next to NULL return base; //return the new end of the list to the caller
//Shouldnt we return newNode? }
/* * Walk the list and print the data */
void printList(struct node *list) { while (list != NULL) { fprintf(stdout, "data: %3d ", list->data); list = list->next; } return; }
struct node * insert(struct node * start, int dataAfter, int newData) { struct node * cur = start; struct node * n; while(cur != NULL && cur->data != dataAfter) { cur = cur->next; } if(cur == NULL) /* Not found, return NULL */ return NULL; /* Found, insert new node */ n = (struct node*)malloc(sizeof(struct node)); n->data = newData; n->next = cur->next; cur->next = n; return n; }
struct node * find(struct node * start, int data) { struct node * cur = start; for(; cur != NULL; cur = cur->next) { if(cur->data == data) /* found */ return cur; } /* Not found */ return NULL; }
int delete(struct node * start, int data) { struct node * cur, * prev /* for the predecessor */; for(prev = NULL, cur = start; cur != NULL; cur = cur->next) { if(cur->data == data) /* node to delete found */ break; prev = cur; } if(cur == NULL) /* not found */ return 0;
/* Since no new head is returned, this function will cause a problem if the root is deleted. There is no way to handle this case with the given prototype for delete. A way to solve this would be to pass start as a struct node ** instead of struct node* */ if(prev != NULL) /* not head */ { prev->next = cur->next; /* create new link */ } free(cur); /* deallocate */ return 0; /* SUCCESS */ }
/* * pass the input file to main, then add the data from the input file * to the list. NB The list starts as an empty list. */
int main(int argc, char **argv) { struct node *root = NULL; // The root of the list struct node *temp = NULL; // struct node *base = NULL; // Placeholder for current end of the list char *inBuf = NULL; // input buffer int data = 0; int data2, tokencount; char command;
FILE * ifp;
inBuf = malloc(100); //get a 100 character buffer for input data if (NULL == inBuf) // Check for success { //Let 'em know we failed to get a buffer fprintf(stderr, "No memory - good bye! "); return -1; } ifp = fopen(argv[1], "rwb"); //Get the filename from the command line if (NULL == ifp) // Check for success { //Let 'em know the filename wasn't found fprintf(stderr, "%s file not found. ", argv[1]); return -1; }
/* * Read the file, then add the data to the list * All the while keep track of the last added node in temp */ while (fscanf(ifp, "%[^ ] ", inBuf) != EOF) /* reading till newline */ { fprintf(stdout, "%s ", inBuf);
/* parse the command */ tokencount = sscanf(inBuf, "%c%d%d", &command, &data, &data2);
switch(command) { case 'i': { if(tokencount == 2) /* normal insert */ { //So add a new node & fill it with data if (NULL == root) // First node add to the list { temp = root = addToList(root, data); } else { // add to the bottom of the list temp = addToList(root, data); } if (NULL == temp) { printf("Failed to add - good bye "); return -1; } else { printf("Successfully added "); } } else if(tokencount == 3) /* insert after */ { temp = insert(root, data, data2); if(temp == NULL) { printf("Failed to add after "); } else { printf("Successfully added "); } } } break; case 'f': { if(find(root, data) == NULL) { printf("Not found "); } else { printf("Found "); } } break; case 'd': { if(delete(root,data)) { printf("Failed to delete "); } else { printf("Successfully deleted "); } } break; case 'p': { printList(root); } break; }
} /*printList(root);*/ fclose(ifp);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
