无法在链表C++中追加数据 我试图在C++中实现链表,但由于某些原因,数据没有插入到列表中。

无法在链表C++中追加数据 我试图在C++中实现链表,但由于某些原因,数据没有插入到列表中。,c++,linked-list,C++,Linked List,struct Node { int number; Node* next; }; Node* getNewNode(); void printList(Node*); void append(Node* , int); int main() { Node* head = new Node; Node* looper = head; std::cout << "\t\tCREATING A LINKED LIST!\n\n\n";

struct Node
{
    int number;
    Node* next;
};

Node* getNewNode();
void printList(Node*);
void append(Node* , int);

int main()
{

    Node* head = new Node;
    Node* looper = head;

    std::cout << "\t\tCREATING A LINKED LIST!\n\n\n";

    while (true)
    {

        int number = 0;

        std::cout << "Enter a number : ";
        std::cin >> number;

        if (number == -1)
        {
            break;
            delete looper;

        }

        looper->number = number;
        looper->next = getNewNode();
        looper = looper->next;

    }

    looper = head;

    printList(looper);

    printf("What operation would you like to perform on linked list?\n1. Append data.\n2. Delete data\n3. Push data\n\n");

    int choice = 0;

    std::cout << "Choice : ";

    std::cin >> choice;

    switch (choice)
    {
        case 1:
        {
            int data = 0;

            std::cout << "Enter data to be appended : ";
            std::cin >> data;
            looper = head;
            append(looper, data);
            break;
        }

        default:

            std::cout << "Invalid Input!";
    }

    printList(looper);

    return 0;
}


Node* getNewNode()
{

    Node* node = new Node;

    node->number = 0;
    node->next = NULL;
    return node;
}

void printList(Node* head)
{

    std::cout << "\n\n[";

    while (head->next != NULL)
    {
        std::cout << head->number ;
        head = head->next;

        if (head->next != NULL)

            std::cout << ", ";
    }

    std::cout << "]\n" << std::endl;

}

void append(Node* head, int data)
{

    while (true)
    {
        if (head->next == NULL)
        {
            head->next = getNewNode();
            head = head->next;
            head->number = data;
            head->next = NULL;
            break;
        }
        else

            head = head->next;
    }

}
使用append函数插入数据后,当我使用printList方法打印列表时,它显示0,而不是我插入的数字。我相信这与通过引用传递有关,因为我在internet上看到的代码将指向结构指针的指针发送给append方法。提前感谢

此行中创建了带有0的项目

looper->next = getNewNode(); // default value is 0
当您输入-1以中断循环时,应该删除最后创建的节点,并在最后一个节点之前为元素设置next=0。添加临时变量prev

您还应该在printList函数中更改while循环,如果列表只有一个元素会发生什么?不会打印任何内容

while (head != NULL)  // test head != 0 instead of head->next
{
    std::cout << head->number ;
    if (head->next)  std::cout << ", ";
    head = head->next;
}

除了@rafix07提供的解决方案外,您还可以针对您的问题使用以下解决方案。下面我重新编写了您的全部代码。我希望有帮助

#include<iostream>
#include<stdio.h>

struct Node
{
    int number;
    Node* next;
};

Node* head = NULL;

void printList();
void append();

int main()
{
    std::cout << "\t\tCREATING A LINKED LIST!\n\n\n";
    bool loop_breaker = true;
    while(loop_breaker)
    {
        printf("What operation would you like to perform on linked list?\n1. Append data.\n2. Delete data\n3. Push data\n4. Exit\n\n");
        int choice = 0;
        std::cout << "Choice : ";
        std::cin >> choice;
        switch (choice)
        {
            case 1:
                append();
                break;
            case 4:
                loop_breaker = false;
                break;
            default:
                std::cout << "Invalid Input!";
                break;
        }
        printList();
    }
    return 0;
}

void printList()
{
    Node *looper = head;
    std::cout << "\n\n[";
    do
    {
        std::cout << looper->number;
        if (looper->next != NULL)
            std::cout << ", ";
        looper = looper->next;
    }while (looper != NULL);
    std::cout << "]\n" << std::endl;
}

void append()
{
    Node *temp = new Node;
    std::cout << "Enter data to be appended : ";
    std::cin >> temp->number;
    temp->next = NULL;
    if(head == NULL)
        head = temp;
    else if(head->next == NULL)
        head->next = temp;
    else
    {
        Node *looper = head;
        while (looper->next != NULL)
        {
            looper = looper->next;
        }
        looper->next = temp;
    }
}

很抱歉,我仍然不明白为什么我插入的数据在添加更改后没有显示出来,我已经测试了1,2,3的代码,然后调用append entering 5,得到了1,2,3,5。这对我来说很有效。但您的实现无法处理列表为空且希望调用append函数的情况。我尝试使用head!=0而不是头部->下一步!=无效的如果您在head->next时只更改了这一行,我现在会遇到分段错误!=NULL yes您将得到分段错误,因为在下一行中您有head=head->next和if head->next!=无效的用我发布的代码更改整个函数。
#include<iostream>
#include<stdio.h>

struct Node
{
    int number;
    Node* next;
};

Node* head = NULL;

void printList();
void append();

int main()
{
    std::cout << "\t\tCREATING A LINKED LIST!\n\n\n";
    bool loop_breaker = true;
    while(loop_breaker)
    {
        printf("What operation would you like to perform on linked list?\n1. Append data.\n2. Delete data\n3. Push data\n4. Exit\n\n");
        int choice = 0;
        std::cout << "Choice : ";
        std::cin >> choice;
        switch (choice)
        {
            case 1:
                append();
                break;
            case 4:
                loop_breaker = false;
                break;
            default:
                std::cout << "Invalid Input!";
                break;
        }
        printList();
    }
    return 0;
}

void printList()
{
    Node *looper = head;
    std::cout << "\n\n[";
    do
    {
        std::cout << looper->number;
        if (looper->next != NULL)
            std::cout << ", ";
        looper = looper->next;
    }while (looper != NULL);
    std::cout << "]\n" << std::endl;
}

void append()
{
    Node *temp = new Node;
    std::cout << "Enter data to be appended : ";
    std::cin >> temp->number;
    temp->next = NULL;
    if(head == NULL)
        head = temp;
    else if(head->next == NULL)
        head->next = temp;
    else
    {
        Node *looper = head;
        while (looper->next != NULL)
        {
            looper = looper->next;
        }
        looper->next = temp;
    }
}