@ayushmittal628_58414ee9c56e4875
you need to maintain a pointer to the node after which you are inserting and the very next nodde
so it is like 2 like a—>b and you need to insert c in between them so you need 2 pointers at a and b and then point a->next=c c->next=b your work is done and take care of the cases when the list is empty
struct Node *addAfter( struct Node *last, int data, int item)
{
if (last == NULL)
return NULL;
struct Node *temp, *p;
p = last -> next;
do
{
if (p ->data == item)
{
temp = ( struct Node *) malloc ( sizeof ( struct Node));
temp -> data = data;
temp -> next = p -> next;
p -> next = temp;
if (p == last)
last = temp;
return last;
}
p = p -> next;
} while (p != last -> next);
cout << item << " not present in the list." << endl;
return last;
}
here last node is the last node of the linked list and last node->next=head