【发布时间】:2013-10-21 08:33:38
【问题描述】:
这是我销毁链表的代码。
void destroy(node *h){
if (h->next!=NULL){
destroy(h->next);
}
free(h);
h=NULL;
}
问题是打印还是输出一堆数字:
11, 2, 15, 3, 9, //销毁前
28495936, 28495968, 28496064, 28496096, 0, //销毁后
很遗憾,由于分配原因,我无法更改 void destroy(node *h) 参数。
我尝试过使用 while 循环方法,但仍然得到相同的结果。我也尝试过向左移动并从末尾删除,但我无法删除最后一个节点。
提前致谢。
--编辑-- 根据要求,这里是打印功能
void print(node* N){
printf("%d, ", N->value);
if (N->next)
print_set(N->next);
if (N == NULL)
printf("Empty Set");
}
【问题讨论】:
-
我们可以看到您用来打印列表的代码吗?
-
你为什么要打印一份你销毁的清单?如果赋值是原型
void destroy(node*),那么你不能在调用destroy()的函数中改变指针,所以你不能让指针变为空来阻止打印。 -
我已经添加了打印代码。
-
在打印任何内容之前进行 NULL 检查。例如, void print( node* N ) { if( N == NULL ) {printf("NULL\t"); return;} // 此处休息打印代码 }
-
@Abhineet 想法不错,但结果还是一样。它没有将 N 检测为 NULL
标签: c pointers linked-list