无法在链表c ++中附加数据

时间:2018-06-09 09:30:58

标签: c++ linked-list

我正在尝试用c ++实现链表,但由于某种原因,数据没有插入列表中。

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而不是我插入的数字。我相信它与传递引用有关,因为我在互联网上看到的代码发送一个指向struct指向append方法的指针。提前致谢

2 个答案:

答案 0 :(得分:1)

在此行中创建0的项目

looper->next = getNewNode(); // default value is 0

当您输入-1来打破循环时,您应删除最后创建的节点,并在最后一个节点之前为元素设置next = 0。添加临时变量prev

Node* prev = 0; // new
while (true)
{
    int number = 0;
    std::cout << "Enter a number : ";
    std::cin >> number;
    if (number == -1)
    {
        prev->next = 0;  // new
        delete looper;  // delete last node 
        break;
    }
    looper->number = number;
    looper->next = getNewNode();
    prev = looper; // new
    looper = looper->next;
}

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

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

答案 1 :(得分:0)

除了@ 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;
    }
}