我必须使用c ++中的链接列表来构建联系人列表。当前它无限循环第三个联系人

为我提供了一个类声明和一个main,并且我必须创建一个类定义而不更改任何一个。我对这些概念非常了解,但是我完全没有语法。通过遍历列表打印每个联系人的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," << phoneNum << endl << endl;

   nextContact1 = new ContactNode(fullName,phoneNum);
   headContact->InsertAfter(nextContact1);
   cin.ignore();

   cout << "Person 3" << endl;
   cout << "Enter name:" << endl;
   getline(cin," << 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,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;
}
hanshuixiao94 回答:我必须使用c ++中的链接列表来构建联系人列表。当前它无限循环第三个联系人

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.
}
本文链接:https://www.f2er.com/3083269.html

大家都在问