链表,无法将节点链接到头
linkedlist, cannot link the node to head
我只是在某个地方迷失了方向,我无法弄清楚我的代码出了什么问题。下面的函数是我将节点放入列表的附加函数。
void AppendNode(struct s_list *list, unsigned int data)
{
if (list == nullptr)
return;
struct s_node *tempHead = list->head;
struct s_node *newNode = new s_node;
newNode->next = nullptr;
while (tempHead != nullptr)
tempHead = tempHead->next;
tempHead = newNode;
}
我调用了这个函数 100 次,它根本不会将新节点与当前列表链接起来。找到问题应该不难,但我太糟糕了。给我一些建议。谢谢。
//*****
感谢您的所有回复,但我仍然有同样的问题。我已经为我的列表分配了头节点,然后将其传递给函数。现在,我更改为直接传递列表负责人,不再列表,但仍然有同样的问题......
void AppendNode(struct s_node *head, unsigned int data)
{
if (head == nullptr)
return;
struct s_node *tempHead = head;
struct s_node *newNode = new s_node;
newNode->next = nullptr;
while (tempHead != nullptr)
tempHead = tempHead->next;
tempHead = newNode;
}
您的tempHead
在列表末尾运行;此函数中的任何内容都不会更改列表。
首先处理空列表的情况:
if(list->head == NULL)
{
list->head = newNode;
return;
}
然后谨慎前进:
while (tempHead->next != nullptr)
tempHead = tempHead->next;
tempHead->next = newNode;
或者,
如果您尝试将节点附加到列表的末尾,只需执行以下操作:
while (tempHead->next != nullptr)
tempHead = tempHead->next;
tempHead->next = newNode;
相关文章:
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 如何确保我的节点被添加到链接列表中
- 链接节点本机模块中的提升库,对象文件需要替代库版本
- 如何遍历具有两个节点的链接节点
- 链接列表中的所有节点都相同,似乎插入不起作用
- 如何在 rust 中静态链接节点.js?
- 链接列表的push_front和push_back方法似乎删除了节点
- 在索引中插入节点:链接列表未正确返回列表
- 链接列表中的节点类的大小为16
- 删除节点并处理新的 nullptr 和链接节点
- 链接节点列表,分段故障
- 节点本机模块 - 链接静态库
- C 指针节点帮助 - 将参数复制到链接的列表节点更改该节点的不同部分
- 如何使用模板在数据结构中链接节点对象
- C 中的双重链接节点构造函数
- 一个链接节点,在一个节点中有两个项目
- 带有一个外部指针的链接节点的循环链