【发布时间】:2016-06-30 03:27:05
【问题描述】:
当传递链表的头部时,这个“在简单链表末尾插入一个元素”的实现有什么问题?
void insert (int x, cel *ini) {
cel *tmp = ini;
while (tmp != NULL)
tmp = tmp->prox;
cel *new = malloc(sizeof(cel));
new->value = x;
tmp->prox = new;
new->prox = NULL;
}
【问题讨论】:
-
请提供其余代码以及您遇到的问题。
-
while 循环迭代直到 tmp 为空,然后您尝试取消引用空指针。您必须循环直到 tmp->prox 为空。
-
您没有检查
malloc()是否成功——这是个问题。如何将第一个元素添加到列表中? -
这是一个非常好的教程,@dkb :D
标签: c algorithm linked-list runtime-error