I created a basic implementation of a linked list and tested your code. It's not particularly pretty, but it works:
#include <stdlib.h>
#include <stdio.h>
struct node { struct node *next; };
struct node* create_node(struct node *next) {
struct node* node = malloc(sizeof(struct node));
node->next = next;
printf("created %p\n", node);
return node;
}
void deleteall(struct node **start)
{
struct node*temp,*curr;
while ((*start)->next!=NULL)
{
temp=*start;
*start=(*start)->next;
free(temp);
printf("free'd %p\n", temp);
curr=(*start);
}
if ((*start)->next==NULL)
{
free(curr);
printf("free'd %p\n", curr);
}
}
int main(int argc, char** argv) {
// create root node
struct node * ll = create_node(0);
// insert three nodes
ll->next = create_node(ll->next);
ll->next = create_node(ll->next);
ll->next = create_node(ll->next);
// delete all nodes (including root)
deleteall(&ll);
return 0;
}
The output is:
$ gcc test.c && ./a.out
created 0xc3c010
created 0xc3c030
created 0xc3c050
created 0xc3c070
free'd 0xc3c010
free'd 0xc3c070
free'd 0xc3c050
free'd 0xc3c030
As you can see, all nodes that are allocated are actually free'd.
Maybe you are confused by the fact, that the root node is not set to 0, but apparantly still contains the remains of the object previously located at that place. You could fix that if you assign 0 to the start pointer after you freed the nodes: *start = 0;
(or by eliminating the additional curr
pointer, as suggested in the other answer).
Consider that you could alternatively delete the nodes of your list recursively:
void delete_node_recursively(struct node *node) {
if (node) {
delete_node_recursively(node->next);
free(node)
}
}
If you want to have the root node set to 0 in the end, you can add a wrapper:
void delete_list(struct node **root) {
delete_node_recursively(*root);
*root = 0;
}