Question: Hello, I need the following code in C. It's builds off of the code below. the directions are under the code. Thank you. A sample

Hello, I need the following code in C. It's builds off of the code below. the directions are under the code. Thank you. A sample output would be greatly appreciated.

/*Code to build off of*/

#include

#include

#include

// structure node for linked list

typedef struct node {

int data;

struct node *next;

} node;

// find function

struct node *find(struct node *start, int data){

struct node *p;

p = start;

while(p != NULL){

if (p->data == data){

return p;

}

p=p->next;

}

return NULL;

}

//insert function

struct node *insert(struct node **start, int dataAfter, int newData){

struct node *q, *p;

p = find(*start, dataAfter);

if (p != NULL){

q = (struct node *)malloc(sizeof(struct node));

q->data = newData;

q->next = p->next;

p->next = q;

return q;

}

return NULL;

}

//delete function

int delete(struct node **start, int data){

struct node *q, *p;

if((*start)->data == data)

{

*start = (*start)->next;

return 1;

}

else

{

q=p=*start;

while(q->data != data && q->next!=NULL)

{

p=q;

q=q->next;

}

if(q->data == data)

{

p->next = q->next;

return 1;

}

else

{

return 0;

}

}

}

struct node * addToList(struct node **base, int data)

{

struct node *newNode;

newNode = (struct node*) malloc(sizeof(struct node));

struct node *temp;

newNode->data = data;

newNode->next = NULL;

if(*base == NULL)

*base = newNode;

else

{

temp = *base;

while(temp->next != NULL)

temp=temp->next;

temp->next = newNode;

}

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;

}

/*

* 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

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

*/

// Code from StackOverflow

int i,j,d1,d2;

char c,value[10]; //command char

while (fgets(inBuf, sizeof(inBuf),ifp))

{

c=inBuf[0];

i = 2;

j=0;

d1=0;

d2=0;

if(c=='p')

{

printList(root);

continue;

}

while(inBuf[i] != '\0')

{

if(inBuf[i]==' ')

{

j=0;

i++;

sscanf(value, "%d", &d1);

if(c=='i')

{

}

continue;

}

value[j]=inBuf[i];

value[j+1]='\0';

j++;

i++;

}

sscanf(value, "%d", &d2);

if(d1>0)

{

if(c=='i')

{

//printf("insert:%d\t%d ",d1,d2);

temp = insert(&root,d1,d2);

if (NULL == temp)

{

printf("Failed to add - good bye ");

return -1;

}

}

}

else

{

if(c=='i')

{

if (NULL == root) // First node add to the list

{

//printf("add to list: %d\t%d ",d1,d2);

root = addToList(&root, d2);

if (NULL == root)

{

printf("Failed to add - good bye ");

return -1;

}

}

else

{

//printf("%d\t%d ",d1,d2);

temp = addToList(&root, d2);

if (NULL == temp)

{

printf("Failed to add - good bye ");

return -1;

}

}

}

if(c=='f')

{

temp = find(root,d2);

if(temp!=NULL)

{

printf("%d - Data Found ",temp->data);

}

else

printf("%d - Data Not Found ",d2);

}

if(c=='d')

{

if(delete(&root,d2)==1)

printf("%d - Deleted Successfully ",d2);

else

printf("%d - Could not Delete ",d2);

}

}

}

printList(root);

fclose(ifp);

return 0;

}

/*Directions*/

Using the code you built, 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.

HINT This is all about pointer manipulation.

The success of the reverse command can be verified by using the "p" or print command from Lab 1.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!