I'm working through some personal test code to make sure I understand how to work withs singly linked lists.
I'm basically going through the Week 5 Section video on singly linked lists and putting code together for each action outlined in the video.
I'm stuck putting the code together that will "destroy" a singly linked list. The idea illustrated in the Section video makes sense (recursion to free each node in the list, from end to beginning).
the initial function call in main is
removeall (Team);
where Team is a pointer to the head of the list.
my current code for the function is below
void removeall(REBEL* Team)
{
if (Team->next == NULL)
{
free(Team);
}
else
{
removeall (Team->next);
}
}
The code runs but when I run Valgrind, it says:
HEAP SUMMARY:
==4951== in use at exit: 72 bytes in 3 blocks
==4951==
total heap usage: 4 allocs, 1 frees, 96 bytes allocated
I don't understand why it is only 1 free? Was hoping for 4 frees.
Any thoughts would be appreciated. Thank you