删除整个链表C.

Sha*_*ars 2 c recursion linked-list

我的程序使用链接列表按顺序输入数字.

My input: 2 4 23 34 534 543 
Run Code Online (Sandbox Code Playgroud)

当我去删除列表时,我得到了这个:

137429056 137428992 137429040 137429024 137429008 0 
Run Code Online (Sandbox Code Playgroud)

这是为什么?

void deleteList(node* head)
{
    if(head == NULL)
        printf("List is empty\n");

    else
        deleteList(head->next);

    free(head);
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*136 6

释放内存,但不要将任何链接(或头部本身)设置为null,因此您将引用未分配的内存.

另外:为什么在while循环更简单时使用递归?