1
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

除了我的删除列表功能外,一切正常。由于某种原因,它在应该释放每个节点时输出垃圾(从而释放整个链表)。每个函数都必须递归完成(这是一个赋值)。

void deleteList(node* head)
{
    if(head)
    {
        deleteList(head->next);
        free(head);
    }
}
4

1 回答 1

2

删除整个列表后,您需要将head指针设置为NULL. 否则它会悬空,当您尝试打印列表时会导致未定义的行为。

为了能够在内部执行此操作deleteList(),您需要将函数更改head为双指针 ( node**)。

于 2013-03-19T07:10:52.813 回答