链表中的无限循环

时间:2013-04-19 16:09:04

标签: c++ loops linked-list nodes infinite

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

Student.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

Student.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;
}

Node.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

Node.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;
}

Driver.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
.
.
.

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

3 个答案:

答案 0 :(得分:2)

这会导致你的无限循环

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之间感到困惑。

答案 1 :(得分:2)

这将无限循环。 curr永远不会改变。

void display() {
    Node * curr = head;

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

你可能打算这样做。

void display() {
    Node * curr = head;

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

答案 2 :(得分:0)

这会导致你的无限循环

while(curr->getNext() != NULL) 
     curr->setNext(curr);

应该是

while(curr != NULL) //find last node on list
         curr = curr->next;
相关问题