C ++链表节点创建导致无限循环
C++ Linked List Node Creation Causing Infinite Loop
我正在尝试创建一个简单的双向链表来熟悉 C++ 中的指针。每个节点包含一个整数、一个指向下一个节点的指针和一个指向前一个节点的指针。当我尝试输出链表对象中每个节点的值时,它会无限期地打印值。
我的测试代码用一个节点初始化一个链表,并添加额外的 3 个节点。
调用 PrintNodeVals() 方法时,while 循环会无限期地迭代,输出节点值的恒定流。当使用 for 循环代替 while 循环时,它打印一次头节点的地址,然后连续打印第二个地址,这是使用 addnode() 方法连接的第一个节点,也是链接中的第二个节点整体上榜。
我能想到的唯一解释是我的代码以某种方式分配了第二个节点 "next" 指向节点本身的指针,这将导致 PrintNodeVals() while 循环始终计算为真。
有什么想法吗?
#include "LinkedList.h"
LinkedList::LinkedList(){
root = new Node();
}
//adds node to the end of the list
void LinkedList::addnode(){
Node newnode;
Node *conductor = root;
while(conductor->next != 0){
conductor = conductor->next; //(*conductor).next
}
conductor->next = &newnode; //makes the next field point to the new node
newnode.prev = conductor;
}
void LinkedList::PrintNodeVals(){
Node *conductor = root;
while(conductor != 0){
std::cout << conductor->val;
conductor = conductor->next;
}
/*
for(int i = 0; i < 10; i++){
std::cout << conductor << "\n";
conductor = conductor->next;
*/
}
}
//TEST CODE
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList linkle;
linkle.addnode();
linkle.addnode();
linkle.addnode();
linkle.ShowNodeVals();
return 0;
}
问题是您在列表中存储了一个指向局部变量的指针:
Node newnode;
// ...
conductor->next = &newnode;
newnode
在块的末尾被销毁,指针变为无效。您可能应该动态分配新节点或使用 std::list
而不是您自己的列表 class.
您应该在创建新节点时分配 space(就此而言,它应该是指向节点的指针)。
请记住,双链线性列表模型应该是将您的节点连接到列表(您指向的列表),然后将列表连接到节点。
Node *newNode = new Node();
newNode->data = element //Set your element in your node here
Node *conductor = root;
while (conductor->next != nullptr) {
conductor = conductor->next;
}
//So you will be connecting your new element like:
newNode->next = nullptr; //Connect node in last position
newNode->prev = conductor; //Connect node to previous position which should be stored by conductor doing that loop
//Then connect the list to the new node
conductor->next = newNode;
此外,您可能需要检查构造函数并确保列表中的第一个元素(在此处创建)在两侧都指向 NULL。
请记住,这仅在您将元素添加到列表的最后一个位置时才有效,如果您要插入位置,那么您应该考虑各种情况以编写一些真正多汁且漂亮的代码!
希望对您有所帮助!
P.D:如果您需要更多帮助,请发送消息。 :)
我正在尝试创建一个简单的双向链表来熟悉 C++ 中的指针。每个节点包含一个整数、一个指向下一个节点的指针和一个指向前一个节点的指针。当我尝试输出链表对象中每个节点的值时,它会无限期地打印值。
我的测试代码用一个节点初始化一个链表,并添加额外的 3 个节点。
调用 PrintNodeVals() 方法时,while 循环会无限期地迭代,输出节点值的恒定流。当使用 for 循环代替 while 循环时,它打印一次头节点的地址,然后连续打印第二个地址,这是使用 addnode() 方法连接的第一个节点,也是链接中的第二个节点整体上榜。
我能想到的唯一解释是我的代码以某种方式分配了第二个节点 "next" 指向节点本身的指针,这将导致 PrintNodeVals() while 循环始终计算为真。
有什么想法吗?
#include "LinkedList.h"
LinkedList::LinkedList(){
root = new Node();
}
//adds node to the end of the list
void LinkedList::addnode(){
Node newnode;
Node *conductor = root;
while(conductor->next != 0){
conductor = conductor->next; //(*conductor).next
}
conductor->next = &newnode; //makes the next field point to the new node
newnode.prev = conductor;
}
void LinkedList::PrintNodeVals(){
Node *conductor = root;
while(conductor != 0){
std::cout << conductor->val;
conductor = conductor->next;
}
/*
for(int i = 0; i < 10; i++){
std::cout << conductor << "\n";
conductor = conductor->next;
*/
}
}
//TEST CODE
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList linkle;
linkle.addnode();
linkle.addnode();
linkle.addnode();
linkle.ShowNodeVals();
return 0;
}
问题是您在列表中存储了一个指向局部变量的指针:
Node newnode;
// ...
conductor->next = &newnode;
newnode
在块的末尾被销毁,指针变为无效。您可能应该动态分配新节点或使用 std::list
而不是您自己的列表 class.
您应该在创建新节点时分配 space(就此而言,它应该是指向节点的指针)。
请记住,双链线性列表模型应该是将您的节点连接到列表(您指向的列表),然后将列表连接到节点。
Node *newNode = new Node();
newNode->data = element //Set your element in your node here
Node *conductor = root;
while (conductor->next != nullptr) {
conductor = conductor->next;
}
//So you will be connecting your new element like:
newNode->next = nullptr; //Connect node in last position
newNode->prev = conductor; //Connect node to previous position which should be stored by conductor doing that loop
//Then connect the list to the new node
conductor->next = newNode;
此外,您可能需要检查构造函数并确保列表中的第一个元素(在此处创建)在两侧都指向 NULL。
请记住,这仅在您将元素添加到列表的最后一个位置时才有效,如果您要插入位置,那么您应该考虑各种情况以编写一些真正多汁且漂亮的代码!
希望对您有所帮助!
P.D:如果您需要更多帮助,请发送消息。 :)