首页 > 解决方案 > 链表插入和打印 C++

问题描述

所以我试图使用双指针来创建插入函数,然后打印链接列表。

我设法用单指针做到了,但是这个双指针让我发疯了。

#include <iostream>
#include <string>

using namespace std;

class Node {
public:
   string name;
   int ID;
   int marks[10];
   Node *next;
};

void printOptions() {
    cout << endl;
    cout << "1.Insert New Node" << endl;
    cout << "2.Print List" << endl;
    cout << "3.Exit" << endl;
}

void insertAtBack(string inputName, Node **headref) {
    Node **currentNodeRef;
    currentNodeRef = headref;

        while ((*currentNodeRef)->next != NULL) {
             (*currentNodeRef) = (*currentNodeRef)->next;
    }
    (*currentNodeRef)->next = new Node();
    (*currentNodeRef)->next->name = inputName;
    (*currentNodeRef)->next->next = NULL;

 }

 void printList(Node *head) {
    Node *indexNode;
    indexNode = head;
    while (indexNode != NULL) {
        cout << (indexNode)->name << endl;
        (indexNode) = (indexNode)->next;
    }
  }

 int main() {

       cout << "This implements a linked list" << endl;

       int option;
       bool infinite = true;

       Node *head = NULL;
       string testName;

       while (infinite == true) {
         printOptions();
         std::cin >> option;


         switch (option) {

         case 1:
            cout << "Enter student name" << endl;
            std::cin >> testName;
            if (head == NULL) {
                 head = new Node();
                 head->name = testName;
            }
            else {
                 insertAtBack(testName, &head);
            }
            break;

         case 2:
             printList(head);
             break;

         case 3:
             exit(1);
             break;
         default:
             exit(1);
             break;
         }

       }
     return 0; 
    }

所以没有编译错误或段错误,而是代码运行它接受 2 个值并很好地打印它们。当输入另一个值时,它不再打印 2 个值。我认为打印功能很好,因为它以前使用单指针工作,但我不是 100% 确定。我认为问题出在插入功能中,但我不知道在哪里。

标签: c++data-structureslinked-list

解决方案


void insertAtBack(string inputName, Node **headref) {
    Node **currentNodeRef;
    currentNodeRef = headref;
    ...

Node **currentNodeRef = headref;是一个错误。请记住,您传递的是指针的地址。你的意思是写:

Node *currentNodeRef = *headref;

并像这样更改功能:

void insertAtBack(string inputName, Node **head) 
{
    Node *tail = *head;
    while(tail->next != NULL)
        tail = tail->next;
    tail->next = new Node();
    tail->next->name = inputName;
    tail->next->next = NULL;
}

也不要忘记初始化head->next = nullptr;

if (head == NULL) {
                 head = new Node();
                 head->name = testName;
                 head->next = nullptr; <--- add
            }

但是,如果insertAtBack准备在ishead时处理会更好。你传递的全部原因是你想要一个对指针的引用,以便你可以初始化它。因此,您可以将代码修改为:headNULLNode **head

void insertAtBack(string inputName, Node **head) 
{
    Node *new_node = new Node();
    new_node->name = inputName;
    new_node->next = nullptr;

    if(*head)
    {
        Node *tail = *head;
        while(tail->next)
            tail = tail->next;
        tail->next = new_node;
    }
    else
    {
        *head = new_node;
    }
}

void printList(Node *head) 
{
    Node *node = head;
    while(node) 
    {
        cout << node->name << endl;
        node = node->next;
    }
}

int main() 
{
    cout << "This implements a linked list" << endl;
    Node *head = NULL;
    string testName;
    while(true) 
    {
        printOptions();
        int option;
        std::cin >> option;
        switch(option) 
        {
        case 1:
            cout << "Enter student name" << endl;
            std::cin >> testName;
            insertAtBack(testName, &head);
            break;
        case 2: printList(head); break;
        case 3: exit(1); break;
        default: exit(1); break;
        }
    }
    return 0;
}

推荐阅读