Reputation: 4185
ccarrill@ubuntu:~/C$ ./ccarri7lab9
q: quit the program
i: inserts an integer value
d: deletes an integer value
c: checks if an integer value is in the list
e: deletes all integers from the list
l: lists the items in the list (small to big)
r: lists the items in reverse order (big to small)
Please make a choice: i
Enter the value you want to insert: 3
Please make another choice: i
Enter the value you want to insert: 4
Please make another choice: i
Enter the value you want to insert: 5
Please make another choice: l //lists integers
3 4 5
Please make another choice: e //deletes list
Please make another choice: l //lists integers
137904144 137904160 0 // output
Please make another choice: q
Everything works fine besides my delete list function. For some reason it's outputting garbage when it should be freeing each node (thus freeing the entire linked list). Each function has to be done recursively (this is an assignment).
void deleteList(node* head)
{
if(head)
{
deleteList(head->next);
free(head);
}
}
Upvotes: 1
Views: 67
Reputation: 500317
After you've deleted the entire list, you need to set the head
pointer to NULL
. Otherwise it's left dangling, leading to undefined behaviour when you try to print the list.
To be able to do that inside deleteList()
, you'll need to change the function to take head
as a double pointer (node**
).
Upvotes: 2