【问题标题】:How to update data in linked list?如何更新链表中的数据?
【发布时间】:2018-05-11 05:46:21
【问题描述】:

我在空闲时间学习链表。所以现在我想学习如何修改用户在链表中输入的数据,但我对这些指针有点迷茫。该程序运行但在我进入年龄后程序停止工作。我认为问题出在 while 循环中的 current=current->next; 上,但我不知道除了 next 之外,当前还应该指向哪里。

void node::update()
{
    int updateAge;
    cout << "Please enter age: ";
    cin>>updateAge;
    int ch;
    node *current = start_ptr;
    node *temp;

    if (start_ptr == NULL)
        cout << "No record to update!" << endl;
    else
    {
        current = start_ptr;

        while((current!=NULL) && (current->temp->age!=updateAge))
        {
            current=current->next; // Is this correct?
        }

        if (current==NULL)
            {cout<<"The Requested age is Not Found" << endl;}


        else if(current->temp->age==updateAge)
        {
            cout<<"What Information You Want To Update?" << endl;
            cout<<"1. Name" << endl;
            cout<<"2. Height" << endl;
            cin>>ch;
            system("cls");

            switch(ch)
            {
                case 1 :
                {
                    cout << "Enter New Name: ";
                    cin.ignore();
                    getline(cin, current->temp->name);
                    break;
                }

                case 2 :
                {
                    cout<<"Enter New Height: ";
                    cin >> current->temp->height;
                    break;
                }

                default:
                {
                    cout<<"Wrong Input! Please choose again: ";
                    cin>>ch;
                }
            }
            cout<<"RECORD UPDATED !";
        }
    }
}

struct list
{
    list *head, *tail;
    list *next;
};

class node
{
    private:
        std::string name; // Name
        int age; // Age in integer
        float height; // In meters

    public:
        node *next; // Pointer to next node
        node *head, *tail;
        node *start_ptr = NULL; // Start Pointer (root)
        node *temp;
        node *temp2;
        node *pNextValue;
        node* prev; // empty header

        void update();
        void printList();
        void delete_end_node();
        void search();
        void sort_age();

    node()
        {
            head = NULL;
            tail = NULL;
        }


        void getInput()
        {
            temp = new node;
            cout << "Name: ";
            cin >> temp->name;
            cout << "Age: ";
            cin >> temp->age;
            cout << "Height: ";
            cin >> temp->height;

            cout<<"\n";
            temp->next = NULL; // Sets the node to be the last node
            if (start_ptr == NULL)
                start_ptr = temp;
            else
            {
                temp2 = start_ptr; // We know temp2 is not NULL - list not empty!
                while (temp2->next != NULL) // The loop will terminate when temp2
                    temp2 = temp2->next; // points to the last node in the list
                // Move to next link in chain
                temp2->next = temp; // Sets the pointer from that last node to point
                                    // to the node that has just declared
            }
        } // End of getInput() function
}; //End of class

【问题讨论】:

  • 请详细说明“程序停止工作”是什么意思。
  • @ChiefTwoPencils 我想通过使用int age 的数据之一来更新数据。所以在update()函数中程序会要求用户输入年龄,但是用户输入年龄后程序立即停止工作
  • 很难搞定你在做什么。 node 非常复杂。它只需要基本的人员统计信息和nextprev 指针。我建议搜索堆栈溢出并查看人们尝试过的其他一些链表实现。一种建议是将链表与列表中包含的数据隔离开来。这使您可以单独测试一个,并减少一次必须处理的错误数量。
  • @user4581301 感谢您抽出宝贵时间,但我已经想通了。我只需要删除所有 temp.

标签: c++ class linked-list


【解决方案1】:

我认为你的 while 条件有问题 **尝试删除 temp 然后尝试我认为它会工作 **

条件应该是 current->age!=updatAge

【讨论】:

  • 非常感谢~事实证明我需要删除update()函数中的所有temp
  • 也可以从 update() 中删除,因为它不需要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多