I was given a class declaration and a main and I must create a class definition without altering either. I understand the concepts pretty well, but I don't have the syntax down at all. The while loop that prints each contact by traversing the list is infinite, it prints the last contact given by the user endlessly, so either the nodes are arranged in a way I am not comprehending(is the head pointer in the back?), or I am Incorrectly assigning directions.
//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;
}
You're setting nextNode
properly but then you're taking that node, which is set to nodePtr
(i.e current node) right back to itself. In other words you're doing the following.
void ContactNode::InsertAfter(ContactNode* nodePtr) {
nextNodePtr = nodePtr; // Set next to the target.
nodePtr->nextNodePtr = nodePtr; // Set next for target to itself.
}