2013-03-19 63 views
1
[email protected]:~/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); 
    } 
} 
+0

在'free'ing之前,设置'head-> next'。 free 'ing后,将'head'设置为NULL。 – 2013-03-19 07:14:16

回答

2

你删除整个列表后,您需要将head指针设置为NULL。否则,它会悬空,导致undefined behaviour当您尝试打印列表。

为了能够在deleteList()中做到这一点,您需要更改函数head作为双指针(node**)。

+0

我也试过,它仍然输出垃圾。我会再试一次。感谢您的快速回复 – juice 2013-03-19 07:12:49

+0

没关系..我是个白痴。我有head == NULL而不是head = NULL:\谢谢。 – juice 2013-03-19 07:13:56

相关问题