链表插入和打印C ++

时间:2019-06-09 17:31:11

标签: c++ data-structures linked-list

因此,我尝试使用双指针创建插入函数,然后打印链接列表。

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

#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%。 我认为问题出在插入函数中,但我不知道在哪里。

1 个答案:

答案 0 :(得分:0)

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

但是,如果headheadNULL准备好处理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; } 的全部原因是因为您想要引用指针,以便可以对其进行初始化。因此,您可以将代码修改为:

tee