【发布时间】:2016-06-27 10:57:34
【问题描述】:
下面是C语言代码:
函数调用:
insert(&head,value);
void insert(struct node** headref,int value)
{
struct node* head = (*headref);
while( head!=NULL )
{
head= head->link;
}
struct node* new_node=(struct node*)malloc( sizeof(struct node) );
new_node->data=value;
new_node->link=NULL;
head=new_node;
}
【问题讨论】:
-
列表应该如何知道新元素?您必须将列表中最后一项的
link成员设置为指向新元素。一旦head变为NULL,您就失去了需要设置link成员的元素。更好的循环终止条件是while (head->link != NULL) -
我建议你永远不要移动你的
head,因为不推荐!它是一个引用指针,应该始终指向链表中的起始节点。请改用其他指针。
标签: c data-structures linked-list singly-linked-list