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

标签 c++ pointers linked-list nodes contacts

我得到了一个类声明和一个 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;
}

最佳答案

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.
}

关于c++ - 我必须使用 C++ 中的链接列表构建联系人列表。目前无限循环第三个联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58908840/

相关文章:

C++ 重载 : Overloading the [][] operator

c++ - void *function() 是指向函数的指针还是返回 void* 的函数?

C++:无法使用类型为 'char**' 的右值初始化类型为 'char*[x]' 的变量

c++ - 使用递归查找单个链表中倒数第 n 个节点

c++ - 使用另一个类对象的 std::vector 类

c++ - 来自 Tsai 校准参数的 3x3 单应矩阵

C++抛出错误

C - 包含链表的数组

java - Junit 实现 Iterable

c - 我的链表实现有什么问题?