単一のリンクリストを実装しています。プログラムを実行した瞬間に、無限ループが発生します: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
学生.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
.
.
.
非常に長いコードとええと..愚かな質問で申し訳ありません。ありがとうございました