从用户获取链接列表的值,直到用户按下0,然后以c ++显示输出

问题描述

在此程序中,我想从链接列表中的用户获取价值,直到用户输入 0 并显示该链接列表的输出。我认为出现的问题是值覆盖了相同的节点。我正在输入功能 void create_node()。请指导以发现错误!

#include <iostream>
using namespace std;

class node
{
public:
    int ID;

    node *next;         
};

class line
{
private:
    node *head,*tail;          
public:
    line ()
    {
        head = tail = NULL;         
    }

    void create_node()
    {
        node *my_node = new node;
        int a;

        for(;;)
        {
            cout<<"Enter a value to Liked List:";
            cin>>a;
            my_node-> ID = a;
            my_node-> next = NULL;

            if(my_node-> ID!=0)
            {
                if(!head)
                {
                    head = tail = my_node;
                }
                else
                {
                    tail-> next = my_node;
                    tail = tail-> next;
                }
            }
            else if(my_node-> ID==0)
            {
                cout<<"0 come"<<endl;
                break;
            }
        }
    }
    // create_node end here

    void display()          
    {
        node *t = head;                 
        if(!head)               
        {
            cout<<"Linked list not exists!!!"<<endl;
        }
        else
        {
            while (t!=NULL)
            {
                cout<<"ID: "<<t->ID<<endl;

                cout<<"------------------------"<<endl;

                t = t-> next;   
            }
        }
    }

    /*
    void Search(int number)
    {
        node *tmp = head;
        while (tmp != NULL)
        {
            if(number == tmp->ID)
            {
                cout << "number " <<tmp->ID << " is found!" << endl;
                break;
            }
            tmp = tmp->next;
        }
    }
    */
};

int main ()
{
    line obj;
    obj.create_node();

    obj.display();

    system ("pause");
    return 0;
}

解决方法

node *my_node = new node;

如果要继续创建新节点,它应该在for循环内。如果您不希望为0创建新节点,则应该在创建新节点之前检查用户是否输入了0。

    int a;

for(;;)
{
    cout<<"Enter a value to Liked List:";
    cin>>a;
    node *my_node = new node;

...

如果未创建新节点,则它将继续覆盖同一节点,并且将具有0

正如评论中指出的那样,更好地命名该函数以反映内部正在执行的操作。

,

您不会为用户输入的每个值创建一个新节点。您仅创建1个节点,并且每次确实覆盖它。

您应该将用户输入全部从create_node()中移出。它不属于那里。请改用main()中的用户输入。

尝试更多类似的方法:

#include <iostream>
#include <cstdlib>
using namespace std;

class node
{
public:
    int ID;
    node *next;
};

class linkedList
{
private:
    node *head,*tail;

public:
    linkedList()
    {
        head = tail = NULL;
    }

    ~linkedList()
    {
        node *tmp = head;
        while (tmp) {
            node *next = tmp->next;
            delete tmp;
            tmp = next;
        }
    }

    void create_node(int number)
    {
        node *my_node = new node;
        my_node->ID = number;
        my_node->next = NULL;

        if (!head) head = my_node;
        if (tail) tail->next = my_node;
        tail = my_node;
    }

    void display()
    {
        if (!head)
        {
            cout << "Linked list is empty!!!"<< endl;
        }
        else
        {
            node *tmp = head;
            do
            {
                cout << "ID: "<< tmp->ID << endl;
                cout << "------------------------" << endl;
                tmp = tmp-> next;
            }
            while (tmp);
        }
    }

    /*
    void Search(int number)
    {
        node *tmp = head;
        while (tmp)
        {
            if (number == tmp->ID)
            {
                break;
            }
            tmp = tmp->next;
        }
        if (tmp) {
            cout << "number " << number << " is found!" << endl;
        } else {
            cout << "number " << number << " is not found!" << endl;
        }
    }
    */
};

int main()
{
    LinkedList obj;
    int a;

    for(;;)
    {
        cout << "Enter a value to add to Linked List: ";
        cin >> a;

        if (a != 0)
        {
                obj.create_node(a);
        }
        else
        {
            cout << "0 come" << endl;
            break;
        }
    }

    obj.display();

    system("pause");
    return 0;
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...