I am trying to traverse through a linked list by name and then delete the matching name. I'm having some problems understanding why it isn't working like I want it to.
If I am typing in names: fred, roy, ben, rory.
It will be layed out like this: rory - ben - roy - fred - NULL
Now, I'm attempting to remove fred, and then trying to display the list again. It's only displaying "roy". And if I remove only "roy" - it will remove "roy" and "rory", and only display ( ben , fred ).
My explanation is probably just confusing you more. I need to understand why this is happening and how I can solve it.
You're returning p_current from the delete function back to main to be the new p_people value. p_current might be in the middle of the list if you had to traverse through a few nodes to find the name you want to delete.
So when you go to view the list after deleting, p_people might be pointing to a node in the middle of the list. Or - if you go to add a new person, the new node will be added in the middle and you'll lose access to the original earlier part of the list.
I'd at least maintain a pointer that always points to the first node to the list. It will need to be updated when a new node is added or when the first node is removed. Then you can always use this pointer to find the front of the list.
Sometimes it's referred to as the head or root. There are a couple of examples here: