C链表尾部插入节点
我在使用 C 中的链接列表的插入方法时遇到了一些问题。它似乎只在列表的开头添加。我所做的任何其他插入都会失败。而且这个 CodeBlocks 调试器太难理解了,我还是不明白。它从来没有给我价值,只是内存中的地址。无论如何,这是我的职责。您看到它失败的任何原因吗?
/* function to add a new node at the end of the list */
int addNodeBottom(int val, node *head){
//create new node
node *newNode = (node*)malloc(sizeof(node));
if(newNode == NULL){
fprintf(stderr, "Unable to allocate memory for new node\n");
exit(-1);
}
newNode->value = val;
//check for first insertion
if(head->next == NULL){
head->next = newNode;
printf("added at beginning\n");
}
else
{
//else loop through the list and find the last
//node, insert next to it
node *current = head;
while(current->next != NULL)
{
if(current->next == NULL)
{
current->next = newNode;
printf("added later\n");
}
current = current->next;
}
}
return 0;
}
然后在main中,只添加了929。
//testing addNodeBottom function
addNodeBottom(929, head);
addNodeBottom(98, head);
addNodeBottom(122, head);
addNodeBottom(11, head);
addNodeBottom(1034, head);
I'm having some trouble with my insertion method for a linked list in C. It seems to only add at the beginning of the list. Any other insertion I make fail. And this CodeBlocks debugger is so hard to understand I still don't get it. It never gives me value, just addresses in memory. Anyway this is my function. Do you see any reason why it's failing?
/* function to add a new node at the end of the list */
int addNodeBottom(int val, node *head){
//create new node
node *newNode = (node*)malloc(sizeof(node));
if(newNode == NULL){
fprintf(stderr, "Unable to allocate memory for new node\n");
exit(-1);
}
newNode->value = val;
//check for first insertion
if(head->next == NULL){
head->next = newNode;
printf("added at beginning\n");
}
else
{
//else loop through the list and find the last
//node, insert next to it
node *current = head;
while(current->next != NULL)
{
if(current->next == NULL)
{
current->next = newNode;
printf("added later\n");
}
current = current->next;
}
}
return 0;
}
Then in main, only 929 is added.
//testing addNodeBottom function
addNodeBottom(929, head);
addNodeBottom(98, head);
addNodeBottom(122, head);
addNodeBottom(11, head);
addNodeBottom(1034, head);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这段代码会起作用。 Samplebias 的答案几乎是正确的,但您需要第三个更改:
更改 1:
newNode->next
必须设置为NULL
,这样我们就不会插入无效指针在列表的末尾。更改2/3:循环改为无限循环,当我们找到最后一个元素时,将用
break;
跳出。请注意while(current->next != NULL)
与之前的if(current->next == NULL)
是如何矛盾的。编辑:关于 while 循环,这种方式要好得多:
This code will work. The answer from samplebias is almost correct, but you need a third change:
Change 1:
newNode->next
must be set toNULL
so we don't insert invalid pointers at the end of the list.Change 2/3: The loop is changed to an endless loop that will be jumped out with
break;
when we found the last element. Note howwhile(current->next != NULL)
contradictedif(current->next == NULL)
before.EDIT: Regarding the while loop, this way it is much better:
malloc
一个node
后,请确保设置node->next = NULL
。我应该指出,在这个版本中,
head
仍然用作虚拟对象,而不是用于存储值。这使您可以通过只有一个head
节点来表示一个空列表。After you
malloc
anode
make sure to setnode->next = NULL
.I should point out that with this version the
head
is still used as a dummy, not used for storing a value. This lets you represent an empty list by having just ahead
node.我知道这是一篇旧文章,但仅供参考。以下是如何在不对空列表进行特殊情况检查的情况下进行追加,但代价是代码看起来更复杂。
I know this is an old post but just for reference. Here is how to append without the special case check for an empty list, although at the expense of more complex looking code.
我想在写代码之前提一下关键,供大家考虑。
//key
temp= malloc 函数分配的新节点地址(C 中 alloc.h 库的成员)
prev= 现有链表最后一个节点的地址。
next = 包含下一个节点的地址
I would like to mention the key before writing the code for your consideration.
//Key
temp= address of new node allocated by malloc function (member od alloc.h library in C )
prev= address of last node of existing link list.
next = contains address of next node
新节点始终添加在给定链表的最后一个节点之后。例如,如果给定的链表是 5->10->15->20->25,我们在末尾添加一个项目 30,那么链表将变为 5->10->15- >20->25->30。
由于链表通常由其头表示,因此我们必须遍历链表直到结束,然后将最后一个节点的下一个节点更改为新节点。
The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.
这工作正常:
示例用法:
This works fine:
Example usage: