gpt4 book ai didi

c++ - 单链表显示函数中的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:52:12 24 4
gpt4 key购买 nike

我正在尝试实现我的显示功能(在我的 main.cc 文件中)。然而,当我使用我的 curr 指针在我的 Student 对象上获取数据然后遍历列表时,程序核心转储。

主要.CC

#include "Node.h"
#include "Student.h"

using namespace std;

void append (Node **, Node *); //append Node to end of list.
void display(Node *); //print linked list.
void input(Student *); //enter new Student object.
void deleteNode(Node **, string); //delete specific Node.

string first;
string mid;
string last;
string ssn;
string age;

int main() {

string flag; //flag checks for delimiter.
string name;
string stuDelete; //name to be deleted.

Student * stuPtr = NULL;
Node * head = NULL;
Node * newPtr = NULL;

do {
stuPtr = new Student;
input(stuPtr);
flag = stuPtr ->getFirst(); //flag = first name

if(stuPtr -> getFirst() == "-") { //"-" is delimiter
cout << "in if statement\n";
delete stuPtr; //no new Student, no more need for
//temporary Student pointer.
stuPtr = NULL;
cout << "checkpoint1" << endl;
}
else {
newPtr = new Node(stuPtr);
append(& head, newPtr);
cout << "checkpoint 2" << endl;
}
} while (first != "-"); //will prompt for more entries unless
//delimiter is detected.

cout << "checkpoint 3" << endl;
display(head); // MY DISPLAY FUNCTION SUCKS

if(head)
cout << "Enter a Student to be deleted" << endl;
while(head) {
cout << "Last name: ";
cin >> stuDelete;
deleteNode(& head, stuDelete);

string reqDelete; // asks if you want to keep deleting.
cout << "Student has been deleted. Delete another? (Y/N)" << endl;
cin >> reqDelete;

if(head != NULL && reqDelete == "Y") {
display(head); //iterates thru linked list
cout << "\nEnter another name to be deleted: \n" << endl;
}
else if(reqDelete != "Y")
cout << "Deletion complete.\n" << endl;
}

if(head)
display(head);
else
cout << "The list is now empty.\n"
<< "=============================" << endl;

return 0;

}

void display (Node * newPtr) {

Node * curr = newPtr;

while(curr != NULL) {
// getData is used to point to the stud info in a node
cout << "{"
<< curr->getData()->getFirst() << ", "
<< curr->getData()->getMiddle() << ", "
<< curr->getData()->getLast()<< ", "
<< curr->getData()->getSocial()<< ", "
<< curr->getData()->getAge()
<< "}" << endl;
curr = curr->getNext(); // move to the next obj, traverse stud data
}
cout << "-----------------------------------------" << endl;
}

我将列出我的 Node 和 Student 类的标题。如果信息不够,请告诉我。

节点.H

class Node {

public:
Node(); //Default constructor.
Node(Student *); //New constructor.

Student * getData(); //get data on Student object.
void setData(Student *); //set data for Student object.

Node * getNext(); //get the next Node in the linked list.
void setNext(Node * ); //set the next Node in the list.

private:
Student * data; //pointer to current Student data.
Node * next; //pointer within Node to the next Node.

};

#endif

学生.H

class Student {

public:
Student(); //Default constructor;
Student(const string &, const string &, const string &,
const string &, const string &); //New constructor.

//setters
void setName(const string &, const string &, const string &);
void setSocial(const string &);
void setAge(const string &);

//getters
string getFirst();
string getMiddle();
string getLast();
string getSocial();
string getAge();

private:
string stuData[5]; //array of fields for Student data.
};

终端输出

faruki@ubuntu:~/DataStructures/Lab4$ ./a.out

Enter Student information (Enter '-' to exit)
First Name: jesus
Middle Name: h
Last Name: christ
Social: 222222222
Age: 222
==============================
checkpoint 2

Enter Student information (Enter '-' to exit)
First Name: -
Student entry finished.

==============================
in if statement
checkpoint1
checkpoint 3
Segmentation fault (core dumped)

学生类的构造函数

Student::Student() {
stuData[0] = "Firstname";
stuData[1] = "Middlename";
stuData[2] = "Lastname";
stuData[3] = "SSN";
stuData[4] = "##";
}

Student::Student(const string & first, const string & mid,
const string & last, const string & ssn, const string & age) {

setName(first, mid, last);
setSocial(ssn);
setAge(age);
}

节点类的构造函数

Node::Node() {
next = NULL; //new Nodes go to the end of the list.
}

Node::Node(Student * tempData) {
data = tempData;
delete tempData;
next = NULL;
}

最佳答案

  1. 在节点构造函数中,您正在删除 Student 的数据,因此您的 Node::data 指向无效的内存,因此很可能不是显示函数出错,而是在节点构造函数中删除了数据。
  2. 请考虑
    2.1 格式:“.”之间没有空格或“->”(取消引用运算符),因为这会使代码很难阅读
    2.2 使用 C++11 构造,如 nullptr 而不是 NULL
    2.3 将所有 if/for/while/... 放在括号中,因为在维护代码时人们可能会添加一些东西并想知道它是否没有按预期工作。

关于c++ - 单链表显示函数中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17691090/

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