gpt4 book ai didi

c++ - 仅在第二次访问链表中的结构后出现段错误

转载 作者:行者123 更新时间:2023-11-30 03:27:16 25 4
gpt4 key购买 nike

抱歉标题不清楚,我真的不知道如何描述这个问题。我是计算机科学的第一年,所以我对 C++ 还不是很了解。但是,尝试查找此问题并没有帮助。

问题:在主函数中,“printRawData”友元函数被调用了两次。该函数应该打印由“LinkedList”类存储的链表的每个元素。它第一次工作,但第二次出现段错误。我真的不知道我做错了什么。我的助教说他认为结构的字符串变量“element_name”在访问时被破坏了。

如果我没有很好地解释我的问题,或者如果我违反了任何一种 stackoverflow 礼节,那么对于困惑的代码,我们深表歉意。我感谢我得到的任何帮助。

//Note: C++ 11 is needed, due to to_string use
#include <iostream>
#include <string>

using namespace std;

struct Node {
string element_name;
int element_count;
Node* next;
};

class LinkedList{
private:
Node* first;
public:
LinkedList();
~LinkedList();
bool isEmpty();
void AddData(string name, int count);
friend void printRawData(LinkedList l);
};

//where the error occurs
void printRawData(LinkedList l){
Node* n = l.first;
while (n != NULL) { //iterates through the linked list and prints each element
cout << n->element_name << " : " << n->element_count << endl;
n = n->next;
}
}

LinkedList::LinkedList(){
first = NULL;
}

LinkedList::~LinkedList(){
Node* n = first;
while (n != NULL) {
Node* temp = n;
n = temp->next;
delete temp;
}
}

bool LinkedList::isEmpty(){
return first == NULL;
}

void LinkedList::AddData(string name, int count){
Node* newnode = new Node;
newnode->element_name = name;
newnode->element_count = count;
newnode->next = NULL;

Node* n = first;

//if the linked list is empty
if(n == NULL){
first = newnode;
return;
}

//if there's only one element in the linked list,
//if the name of first element comes before the name of new element,
//first element's pointer is to the new element.
//otherwise, the new node becomes the first and points to the previous first
//element.
if (n->next == NULL){
if (n->element_name < newnode->element_name){
n->next = newnode;
return;
} else {
newnode->next = first;
first = newnode;
return;
}
}

//if the first element's name comes after the new element's name,
//have the new element replace the first and point to it.
if (n->element_name > newnode->element_name){
newnode->next = first;
first = newnode;
return;
}

//iterating through linked list until the next element's name comes after
//the one we're inserting, then inserting before it.
while (n->next != NULL) {
if (n->next->element_name > newnode->element_name){
newnode->next = n->next;
n->next = newnode;
return;
}
n = n->next;
}

//since no element name in the linked list comes after the new element,
//the node is put at the back of the linked list
n->next = newnode;
}



main(){
LinkedList stack;

stack.AddData("Fish", 12);
stack.AddData("Dog", 18);
stack.AddData("Cat", 6);

printRawData(stack);
printRawData(stack);
}

最佳答案

函数 void printRawData(LinkedList l) 按值传递参数,因此它获得了 LinkedList 对象的拷贝

但是,拷贝包含 first 指针的拷贝,但不复制任何节点。所以当这个拷贝被销毁时,LinkedList 析构函数将删除所有节点。

然后原件损坏了。

您可能希望传递引用而不是创建拷贝。


这也是 std::list 具有执行“深复制”的复制构造函数和赋值运算符的原因,其中节点也被复制(不仅仅是列表头)。

关于c++ - 仅在第二次访问链表中的结构后出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47521747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com