gpt4 book ai didi

c++ - 链表中的无限循环

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

我正在实现一个单链表。在执行程序的那一刻,我得到一个无限循环:S 不确定到底是什么问题。顺便说一句,由于项目的说明,我不应该实现链接类。无论如何,这是代码(我认为你可以忽略除 Driver.cpp 中的代码之外的所有代码):

学生.h

#ifndef STUDENT_H
#define STUDENT_H

#include <cstdlib>
#include <iostream>

class Student {
public:
Student();
Student(std::string, std::string, char, int, int);

void print(); //displays member functions from Student class

void setFirstName(std::string);
void setLastName(std::string);
void setMiddleName(char);
void setSocialSecurity(int);
void setAge(int);

std::string getFirstName();
std::string getLastName();
char getMiddleName();
int getSocialSecurity();
int getAge();

private:
std::string first_name;
std::string last_name;
char middle_name;
int social_security;
int age;
};
#endif

学生.cpp

#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

#include "Student.h"

Student::Student() {
setFirstName(" ");
setLastName(" ");
setMiddleName(' ');
setSocialSecurity(0);
setAge(0);
}

Student::Student(string fN, string lN, char mN, int SSN, int Age) {
setFirstName(fN);
setLastName(lN);
setMiddleName(mN);
setSocialSecurity(SSN);
setAge(Age);
}

string Student::getFirstName() {
return first_name;
}

string Student::getLastName() {
return last_name;
}

char Student::getMiddleName() {
return middle_name;
}

int Student::getSocialSecurity() {
return social_security;
}

int Student::getAge() {
return age;
}

void Student::setFirstName(string fN) {
first_name = fN;
}

void Student::setLastName(string lN) {
last_name = lN;
}

void Student::setMiddleName(char mN) {
middle_name = mN;
}

void Student::setSocialSecurity(int SSN) {
social_security = SSN;
}

void Student::setAge(int Age) {
age = Age;
}

void Student::print() {
cout << "Full name: " << getFirstName() << ' ' << getMiddleName() << ' ' << getLastName() << endl;
cout << "Age: " << getAge() << endl;
cout << "Social Security Number: " << getSocialSecurity() << endl;
}

节点.h

#ifndef NODE_H
#define NODE_H

#include <cstdlib>

#include "Student.h"

class Node {
public:
Node();
Node(Student *s);

Node *getNext();
Student *getStudent();

void setNext(Node *n);
void setStudent(Student * s);

private:
Student * sPtr;
Node * next;
};
#endif

节点.cpp

#include <iostream>
#include <cstdlib>

using namespace std;

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

Node::Node() {
sPtr = NULL;
next = NULL;
}

Node::Node(Student* s) {
sPtr = s;
next = NULL;
}

Node * Node::getNext() {
return next;
}

Student * Node::getStudent() {
return sPtr;
}

void Node::setNext(Node* n) {
next = n;
}

void Node::setStudent(Student* s) {
sPtr = s;
}

驱动.cpp

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

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

Node * head = NULL;

void mainMenu();
void append(Node *n);
void input();
void display();

void mainMenu()
{
cout<<endl;
cout<<"+++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<" MAIN MENU "<<endl;
cout<<"[A]dd a record "<<endl;
cout<<"[V]iew all records "<<endl;
cout<<"[Q]uit program "<<endl;
cout<<"+++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<endl;
}

void input() {
string fN;
string lN;
char mN;
int socialSec;
int Age;

cout << "Input data... " << endl;
cout << "First name: ";
cin >> fN;
cout << "Last name: ";
cin >> lN;
cout << "Middle initials: ";
cin >> mN;
cout << "Social security number: ";
cin >> socialSec;
cout << "Age: ";
cin >> Age;

Student * sPtr = new Student(fN, lN, mN, socialSec, Age);
Node * nPtr = new Node(sPtr);
append(nPtr);
}

void display() {
Node * curr = head;

while(curr) {
curr->getStudent()->print();
curr->setNext(curr);
}
return;
}

void append(Node * n) {
Node * curr = head;

if(curr == NULL)
head = n;
else {
while(curr->getNext() != NULL) //find last node on list
curr->setNext(curr);
curr->setNext(n); //insert new node as last node
}
}

int main() {

char choice;

do {

mainMenu();
cout << "Enter your command: ";
if (cin>>choice) {
switch(choice)
{
case 'A':
input();
break;
case 'V':
display();
break;
} }
}while(choice != 'Q');
cout << endl;
cout << "Terminating program..." << endl;

return 0;
}

Sample output:
+++++++++++++++++++++++++++++++++++++++++++++++
MAIN MENU
[A]dd a record
[V]iew all records
[Q]uit program
+++++++++++++++++++++++++++++++++++++++++++++++

Enter your command: A
Input data...
First name: Tong
Last name: Zhang
Middle initial: T
Social security number: 1234
Age: 20

Full name: Tong T Zhang
Age: 20
Social Security Number: 1234
Full name: Tong T Zhang
Age: 20
Social Security Number: 1234
Full name: Tong T Zhang
Age: 20
Social Security Number: 1234
Full name: Tong T Zhang
Age: 20
Social Security Number: 1234
Full name: Tong T Zhang
Age: 20
Social Security Number: 1234
Full name: Tong T Zhang
Age: 20
Social Security Number: 1234
Full name: Tong T Zhang
Age: 20
.
.
.

对于超长的代码和呃..愚蠢的问题感到抱歉。谢谢

最佳答案

这会导致你的无限循环

void append(Node * n) {
...
while(curr->getNext() != NULL) //find last node on list
curr->setNext(curr);

应该是

void append(Node * n) {
...
while(curr->getNext() != NULL) //find last node on list
curr = curr->getNext();

我猜你只是把 setNext 和赋值给 curr 弄糊涂了。

关于c++ - 链表中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108947/

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