【发布时间】:2016-05-20 12:15:40
【问题描述】:
我在学习链表的时候写了两个函数。首先计算并返回列表中的节点数。第二个在列表末尾添加新节点。你能帮我理解为什么我需要使用“current->next”在addatend函数下面的代码中检查NULL吗?我不必将它用于第一个功能。没有它,我的 exe 会因指针错误而崩溃...
非常感谢您的时间和帮助。
int length(node * head) {
node * current = head;
int count = 0;
while(current != NULL){ // This line works as expected....
count ++;
current = current->next;
}
return count;
}
void addatend (node * head, int value){
node * newnode = (struct node *) malloc(sizeof(struct node));
node * current = head;
while (current != NULL){ // This line would not work?? If I use current->next != NULL it works.....
current = current->next;
}
current->next = newnode;
newnode->data = value;
newnode->next = NULL;
}
【问题讨论】:
标签: c linked-list nodes traversal