如何修改链表中的用户输入?

时间:2018-05-10 04:57:38

标签: c++ class linked-list

我目前正在学习如何在课堂上使用链表。我只能添加,删除,显示和排序数据。

是否可以编辑用户输入的数据?

请看一下代码,这可能是修改它的方法吗?

要修改列表值,我已经实现了一个简单的提示机制,但是有一点问题:

用户进入年龄后,程序会立即停止工作。

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

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

        while((current!=NULL) && (current->temp->age!=updateAge))
        {
            current=current->next;
        }
        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
        node* current;
        void update();
        void printList();
        void delete_end_node();
        void search();
        void sort_age();
        void deletebyAge();
    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

0 个答案:

没有答案