Circular Linked List

I am having issues in implementing the insertion of node in circular linked list, if insertion is done at non-head position.

Please implement the above code, Also help me by explaining what are the things done in insertion of node at non-head position.

Thank you

@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

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.