我必须使用 C++ 中的链接列表构建联系人列表。它目前无限循环第三个联系人
I must build a contact list using a linked list in c++. It currently infintely loops the third contact
我得到了一个 class 声明和一个 main,我必须创建一个 class 定义而不改变任何一个。我非常了解这些概念,但我根本没有语法。通过遍历列表打印每个联系人的 while 循环是无限的,它无休止地打印用户给出的最后一个联系人,所以节点的排列方式我不理解(后面是头指针吗?),或者我错误地分配方向。
//Class Definition
/* You must use the contacts.h file provided here exactly as is, no changes permitted */
#ifndef CONTACTS_H
#define CONTACTS_H
#include <string>
using namespace std;
class ContactNode { //Class definition
public:
ContactNode();
ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc = 0);
void InsertAfter(ContactNode* nodePtr);
string GetName() const;
string GetPhoneNumber() const;
ContactNode* GetNext();
void PrintContactNode();
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
};
#endif
//Main
/* You must use the main file provided here exactly as is, no changes permitted */
#include <iostream>
#include <iomanip>
#include "ContactNode.h"
using namespace std;
int main() {
string fullName;
string phoneNum;
ContactNode* headContact = 0;
ContactNode* nextContact1 = 0;
ContactNode* nextContact2 = 0;
ContactNode* currContact = 0;
cout << "Person 1" << endl;
cout << "Enter name:" << endl;
getline(cin, fullName);
cout << "Enter phone number:" << endl;
cin >> phoneNum;
cout << "You entered: " << fullName << ", " << phoneNum << endl << endl;
//First contact node (head of heap)
headContact = new ContactNode(fullName, phoneNum);
cin.ignore();
cout << "Person 2" << endl;
cout << "Enter name:" << endl;
getline(cin, fullName);
cout << "Enter phone number:" << endl;
cin >> phoneNum;
cout << "You entered: " << fullName << ", " << phoneNum << endl << endl;
nextContact1 = new ContactNode(fullName, phoneNum);
headContact->InsertAfter(nextContact1);
cin.ignore();
cout << "Person 3" << endl;
cout << "Enter name:" << endl;
getline(cin, fullName);
cout << "Enter phone number:" << endl;
cin >> phoneNum;
cout << "You entered: " << fullName << ", " << phoneNum << endl << endl;
nextContact2 = new ContactNode(fullName, phoneNum);
nextContact1->InsertAfter(nextContact2);
cout << "CONTACT LIST" << endl;
currContact = headContact;
while (currContact != 0) { //Currently prints last contact infinitely, never reaching a null pointer?
currContact->PrintContactNode();
currContact = currContact->GetNext();
cout << endl;
}
return 0;
}
//Now begins the part I am meant to create based on main.cpp and the header file
ContactNode::ContactNode() {
}
ContactNode::ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc=0) {
contactName = initName;
contactPhoneNum = initPhoneNum;
this-> nextNodePtr = nextLoc; //I'm not sure what nextLoc is
return;
}
void ContactNode::InsertAfter(ContactNode* nodePtr) {
ContactNode * temp = 0;
temp = this -> nextNodePtr = nodePtr; //I'm not sure whether the insertion is correct
nodePtr -> nextNodePtr = temp;
return;
}
string ContactNode::GetName() const {
return contactName; //Getter
}
string ContactNode::GetPhoneNumber() const {
return contactPhoneNum; //Getter
}
ContactNode * ContactNode::GetNext() {
return this -> nextNodePtr; //Get pointer to next node?
}
void ContactNode::PrintContactNode() {
cout << "Full Name: " << this->contactName << endl << "Phone Number: " << this-> contactPhoneNum << endl;
}
void ContactNode::InsertAfter(ContactNode* nodePtr) {
ContactNode * temp = 0;
temp = this -> nextNodePtr = nodePtr; //I'm not sure whether the insertion is correct
nodePtr -> nextNodePtr = temp;
return;
}
您正在正确设置 nextNode
,但随后您将设置为 nodePtr
(即当前节点)的节点直接带回自身。换句话说,您正在执行以下操作。
void ContactNode::InsertAfter(ContactNode* nodePtr) {
nextNodePtr = nodePtr; // Set next to the target.
nodePtr->nextNodePtr = nodePtr; // Set next for target to itself.
}
我得到了一个 class 声明和一个 main,我必须创建一个 class 定义而不改变任何一个。我非常了解这些概念,但我根本没有语法。通过遍历列表打印每个联系人的 while 循环是无限的,它无休止地打印用户给出的最后一个联系人,所以节点的排列方式我不理解(后面是头指针吗?),或者我错误地分配方向。
//Class Definition
/* You must use the contacts.h file provided here exactly as is, no changes permitted */
#ifndef CONTACTS_H
#define CONTACTS_H
#include <string>
using namespace std;
class ContactNode { //Class definition
public:
ContactNode();
ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc = 0);
void InsertAfter(ContactNode* nodePtr);
string GetName() const;
string GetPhoneNumber() const;
ContactNode* GetNext();
void PrintContactNode();
private:
string contactName;
string contactPhoneNum;
ContactNode* nextNodePtr;
};
#endif
//Main
/* You must use the main file provided here exactly as is, no changes permitted */
#include <iostream>
#include <iomanip>
#include "ContactNode.h"
using namespace std;
int main() {
string fullName;
string phoneNum;
ContactNode* headContact = 0;
ContactNode* nextContact1 = 0;
ContactNode* nextContact2 = 0;
ContactNode* currContact = 0;
cout << "Person 1" << endl;
cout << "Enter name:" << endl;
getline(cin, fullName);
cout << "Enter phone number:" << endl;
cin >> phoneNum;
cout << "You entered: " << fullName << ", " << phoneNum << endl << endl;
//First contact node (head of heap)
headContact = new ContactNode(fullName, phoneNum);
cin.ignore();
cout << "Person 2" << endl;
cout << "Enter name:" << endl;
getline(cin, fullName);
cout << "Enter phone number:" << endl;
cin >> phoneNum;
cout << "You entered: " << fullName << ", " << phoneNum << endl << endl;
nextContact1 = new ContactNode(fullName, phoneNum);
headContact->InsertAfter(nextContact1);
cin.ignore();
cout << "Person 3" << endl;
cout << "Enter name:" << endl;
getline(cin, fullName);
cout << "Enter phone number:" << endl;
cin >> phoneNum;
cout << "You entered: " << fullName << ", " << phoneNum << endl << endl;
nextContact2 = new ContactNode(fullName, phoneNum);
nextContact1->InsertAfter(nextContact2);
cout << "CONTACT LIST" << endl;
currContact = headContact;
while (currContact != 0) { //Currently prints last contact infinitely, never reaching a null pointer?
currContact->PrintContactNode();
currContact = currContact->GetNext();
cout << endl;
}
return 0;
}
//Now begins the part I am meant to create based on main.cpp and the header file
ContactNode::ContactNode() {
}
ContactNode::ContactNode(string initName, string initPhoneNum, ContactNode* nextLoc=0) {
contactName = initName;
contactPhoneNum = initPhoneNum;
this-> nextNodePtr = nextLoc; //I'm not sure what nextLoc is
return;
}
void ContactNode::InsertAfter(ContactNode* nodePtr) {
ContactNode * temp = 0;
temp = this -> nextNodePtr = nodePtr; //I'm not sure whether the insertion is correct
nodePtr -> nextNodePtr = temp;
return;
}
string ContactNode::GetName() const {
return contactName; //Getter
}
string ContactNode::GetPhoneNumber() const {
return contactPhoneNum; //Getter
}
ContactNode * ContactNode::GetNext() {
return this -> nextNodePtr; //Get pointer to next node?
}
void ContactNode::PrintContactNode() {
cout << "Full Name: " << this->contactName << endl << "Phone Number: " << this-> contactPhoneNum << endl;
}
void ContactNode::InsertAfter(ContactNode* nodePtr) {
ContactNode * temp = 0;
temp = this -> nextNodePtr = nodePtr; //I'm not sure whether the insertion is correct
nodePtr -> nextNodePtr = temp;
return;
}
您正在正确设置 nextNode
,但随后您将设置为 nodePtr
(即当前节点)的节点直接带回自身。换句话说,您正在执行以下操作。
void ContactNode::InsertAfter(ContactNode* nodePtr) {
nextNodePtr = nodePtr; // Set next to the target.
nodePtr->nextNodePtr = nodePtr; // Set next for target to itself.
}