0

I am implementing a single linked list. At the moment of executing the program I get an infinite loop :S Not sure what exactly is the problem. Btw, I am not supposed to implement a Link Class due to the project's instructions.. Anyways, here's the code (I think you can prob ignore all of the code except for the one in 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
.
.
.

Sorry for the super long code and for the eh.. stupid question. Thank you

2
  • When do you get an infinite loop? And did you try debugging?
    – vidit
    Apr 19, 2013 at 16:12
  • Sorry, I get an infinite loop when I execute the program, after I am done adding the information to the first node and call the display function
    – Tong Zhang
    Apr 19, 2013 at 16:14

2 Answers 2

2

This causes your infinite loop

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

it should be

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

I guess you just got yourself confused between setNext and assigning to curr.

0
2

This will loop infinitely. curr never changes.

void display() {
    Node * curr = head;

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

You probably intended this.

void display() {
    Node * curr = head;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.