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

问题描述 投票:2回答:1

我获得了一个类声明和一个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, 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;
}
c++ pointers linked-list nodes contacts
1个回答
2
投票
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.
}
© www.soinside.com 2019 - 2024. All rights reserved.