链表插入和打印C++

linked list insert and printing C++

本文关键字:C++ 打印 插入 链表      更新时间:2023-10-16

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

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

#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; 
    }

因此,没有编译错误或 seg 错误,而是代码运行它接受 2 个值并打印它们。 当键入另一个值插入时,它只打印 2 个值不再。我认为打印功能很好,因为它以前可以使用单指针,但我不是 100% 确定。我认为问题出在插入功能上,但我不在哪里。

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准备在head NULL时处理head,那就更好了。传递Node **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;
}