Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the code for a function that inserts a node with info value x before, a

ID: 3552864 • Letter: C

Question


Complete the code for a function that inserts a node with info

value x before, and a node with info value zafter,each node with

info value y in a linked list with first node pointer p. Assume the info

type is int and that x,y and z contain distinct.

The function returns a pointer to the first node of the

modified list.


Node* InsertBeforeAndAfter(Node* p, int x, int y, int z)



Utility function:

Node * MakeNode(intnewVal, Node* successor)

{ Node * tmp = malloc(sizeof(Node));

assert(tmp != NULL);

tmp->info = newVal; tmp->next = successor;

return tmp;

}

Explanation / Answer

node* InsertBeforeAndAfter(node* p, int x, int y, int z)

{

struct node *curr,*prev;

curr=p;

prev=NULL;

while(curr->next!=NULL)

{

if(curr->data==y)

{

  

if(curr==p)

{

head=MakeNode(x,p);

node *temp=MakeNode(z,curr->next);

curr->next=temp;

}

else

{

node *temp1= MakeNode(x,curr);

prev->next=temp1;

node *temp=MakeNode(z,curr->next);

curr->next=temp;

}

}

prev=curr;   

curr=curr->next;

}


return head;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote