链表输出不正确(c++)
Incorrect output of Linked List (c++)
我正在尝试编写 Node
个对象的链表,其中每个 Node
包含:一个字符串 - data
和一个指针 - next
。为了管理列表(例如:add/remove 个节点),我有节点对象:head
、curr
和 temp
。代码编译正常,但结果不是我想要的。
我遇到的问题是使 curr
节点中的 next
指针实际上指向下一个节点。我一直在添加输出语句以缩小错误范围,它似乎来自以下行:addNode()
函数中的 curr.setNext(n);
。
LinkedList.cpp:
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList() {
}
void LinkedList::addNode(value_type addData) {
Node n;
n.setData(addData);
if (head.getData() != "") {
curr = head;
while(curr.getNext() != NULL) {
curr = *(curr.getNext());
}
curr.setNext(n); //This line is not linking the nodes.
cout << "(2) Curr is pointing to: " << curr.getNext() << " , The location of n is:" << &n << endl;
}
else {
head = n;
}
}
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
class LinkedList {
public:
typedef std::string value_type;
LinkedList();
LinkedList(value_type setData);
void addNode(value_type addData);
private:
Node head;
Node curr;
Node temp;
};
#endif
Node.cpp:
#include <iostream>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node() {
data = "";
next = NULL;
}
void Node::setData(value_type setData) {
data = setData;
}
string Node::getData() {
return data;
}
void Node::setNext(Node n) {
next = &n;
cout << "(1) Curr is pointing to: " << next << " , The location of n is:" << &n << endl;
}
Node * Node::getNext() {
return next;
}
Node.h:
#ifndef NODE_H
#define NODE_H
class Node {
private:
typedef std::string value_type;
value_type data;
Node* next;
Node* prev;
public:
Node();
void setData(value_type setData);
value_type getData();
void setNext(Node n);
Node * getNext();
};
#endif
因此,当我使用测试字符串和空白列表多次调用该函数时,cout
语句应该在两个实例中打印出相同的内存地址。然而,这是输出:
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
所以在 Node
class 中,内存地址正确排列。但是一旦它离开 class 并再次打印,它们就不同了。它与 next
和 curr.getNext()
的存储方式不同有关吗?我将如何解决这个问题?
提前致谢!
你是在复制节点,而不是在链表上操作;
这一行复制节点
curr = *(curr.getNext());
尝试将 Node curr;
更改为 Node *curr;
,然后对指针而不是副本进行操作,例如
curr = curr->getNext()
和
curr->setNext(n);
你还需要进行适当的内存管理,所以像这样的代码将无法工作,因为 Node n
是一个局部变量,不会在函数范围之外存活
Node n;
n.setData(addData);
以下功能不正确:
void Node::setNext(Node n) {
next = &n;
cout << "(1) Curr is pointing to: " << next << " , The location of n is:" << &n << endl;
}
您正在存储堆栈上对象的地址。一旦函数 returns.
指针失效
PS您的代码可能还有其他问题。
我正在尝试编写 Node
个对象的链表,其中每个 Node
包含:一个字符串 - data
和一个指针 - next
。为了管理列表(例如:add/remove 个节点),我有节点对象:head
、curr
和 temp
。代码编译正常,但结果不是我想要的。
我遇到的问题是使 curr
节点中的 next
指针实际上指向下一个节点。我一直在添加输出语句以缩小错误范围,它似乎来自以下行:addNode()
函数中的 curr.setNext(n);
。
LinkedList.cpp:
#include <iostream>
#include <cstdlib>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
LinkedList::LinkedList() {
}
void LinkedList::addNode(value_type addData) {
Node n;
n.setData(addData);
if (head.getData() != "") {
curr = head;
while(curr.getNext() != NULL) {
curr = *(curr.getNext());
}
curr.setNext(n); //This line is not linking the nodes.
cout << "(2) Curr is pointing to: " << curr.getNext() << " , The location of n is:" << &n << endl;
}
else {
head = n;
}
}
LinkedList.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
class LinkedList {
public:
typedef std::string value_type;
LinkedList();
LinkedList(value_type setData);
void addNode(value_type addData);
private:
Node head;
Node curr;
Node temp;
};
#endif
Node.cpp:
#include <iostream>
#include <cstdlib>
#include "Node.h"
using namespace std;
Node::Node() {
data = "";
next = NULL;
}
void Node::setData(value_type setData) {
data = setData;
}
string Node::getData() {
return data;
}
void Node::setNext(Node n) {
next = &n;
cout << "(1) Curr is pointing to: " << next << " , The location of n is:" << &n << endl;
}
Node * Node::getNext() {
return next;
}
Node.h:
#ifndef NODE_H
#define NODE_H
class Node {
private:
typedef std::string value_type;
value_type data;
Node* next;
Node* prev;
public:
Node();
void setData(value_type setData);
value_type getData();
void setNext(Node n);
Node * getNext();
};
#endif
因此,当我使用测试字符串和空白列表多次调用该函数时,cout
语句应该在两个实例中打印出相同的内存地址。然而,这是输出:
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
(1) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c9a0
(2) Curr is pointing to: 0x22c9a0 , The location of n is:0x22c960
所以在 Node
class 中,内存地址正确排列。但是一旦它离开 class 并再次打印,它们就不同了。它与 next
和 curr.getNext()
的存储方式不同有关吗?我将如何解决这个问题?
提前致谢!
你是在复制节点,而不是在链表上操作;
这一行复制节点
curr = *(curr.getNext());
尝试将 Node curr;
更改为 Node *curr;
,然后对指针而不是副本进行操作,例如
curr = curr->getNext()
和
curr->setNext(n);
你还需要进行适当的内存管理,所以像这样的代码将无法工作,因为 Node n
是一个局部变量,不会在函数范围之外存活
Node n;
n.setData(addData);
以下功能不正确:
void Node::setNext(Node n) {
next = &n;
cout << "(1) Curr is pointing to: " << next << " , The location of n is:" << &n << endl;
}
您正在存储堆栈上对象的地址。一旦函数 returns.
指针失效PS您的代码可能还有其他问题。