link list not deleteing

This is a discussion on link list not deleteing within the C Programming forums, part of the General Programming Boards category; I get a string from the user and store it in a linked list in proper order and then print ...

link list not deleteing

I get a string from the user and store it in a linked list in proper order and then print the final list. This works fine. However when it comes to deleting the bottom node on the stack, my code doesn't work. Here is what I have:

Code:

/*This code is representative of many revisions to try to get the solution and thus may be far removed from any actual solution.
As I got more desperate to solve it, I simply began changing it a lot and running it and debugging it to see if I could see the answer.
obviously, I did not find the answer.*/
void deleteNode(node_Ptr *listStarter)
{
node_Ptr previPtr, curPtr, temp;
if ((*listStarter)->next==NULL)
{
previPtr=*listStarter;
(*listStarter)->next=NULL;
previPtr=NULL;
free(previPtr);
}
else
{
while (curPtr!=NULL)
{
previPtr=curPtr;
curPtr=curPtr->next;
}
previPtr->next=NULL;
free(curPtr);
}
printf("\n\n");
}

It should delete from the top up but it doesn't work at all. Any help would be appreciated.

> First of all, thanks for the reply. Secondly, it indeed has to be bottom up on the stack.
> Also, your example doesn't "walk" to the end of the linked list.

Would you be able to come to a conclusion regarding which example is right if I told you that you could say the first example uses a first-in-first-out stack, and the second example uses a last-in-first-out stack? The LIFO version builds the entire stack before deleting, so it does walk to the end.

Lists with one node wont be deleted properly because you set the pointer you wanted to delete to NULL before calling free with your pointer as an argument. In a regular run, you should get a segmentation fault.

Lists with more than one node won't be deleted because the code says walk to the end, delete the last node, and then return. If you really have to delete the list from back to front, then the easiest way is to use a stack, and the easiest way to get a stack is with a recursive implementation, like quzah showed you. If you must implement the stack yourself, then transform the list to an array of nodes first and treat it like your stack.

YES! Vart! I figured that out myself at the end of Math class. I came here to post my resolution. I finally got it! Here's the final deleteNode function I came up with that works in case it helps anyone else:

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens