Append to the list in c

Feedback


Solution:

The issue most likely resides within this specific code line.

   while(ptr!=NULL) ptr= ptr->next; //go until the end of the list

You modify

ptr

repeatedly until it matches

NULL

, which is not desirable as you dereference

ptr

immediately after the loop.

Change it to

   while(ptr->next != NULL) ptr = ptr->next;

Attempt to determine if it is effective. Keep in mind that the aim is to locate the node that does not currently have the following node (to enable insertion).

Frequently Asked Questions