You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

链表中用户输入数据修改方法及代码崩溃问题求助

Hey there! Let's figure out why your update function is crashing and get it working properly. First, let's break down the key issues in your code:

1. 局部变量覆盖类成员指针

In your update() function, you declared a local node *start_ptr; which is uninitialized (it holds a random garbage value). This means your check if (start_ptr == NULL) is completely unreliable, and when you try to traverse the list with current = start_ptr, you're accessing invalid memory—this is almost certainly why your program crashes right after entering the age.

2. 错误的节点成员访问方式

You're using current->temp->age to access a node's age, but temp is a member pointer of the node class only used in getInput() to create new nodes. Each node instance already has its own age member, so you should directly use current->age instead.

3. 冗余的struct list定义

The struct list you declared isn't used anywhere in your code—you can safely remove it to clean things up.

4. 不完善的默认分支逻辑

In your switch statement's default case, you just read a new ch value but don't re-process it. This means after entering an invalid option, the function will skip the update and exit immediately instead of letting the user try again.

5. 重复的类成员指针

Your node class has duplicate pointer members like head, tail, current, etc., which can lead to confusion and unintended state changes. We can simplify this by keeping only the necessary pointers.


修复后的代码

Here's the revised version of your code with all the issues addressed:

#include <iostream>
#include <string>
#include <cstdlib> // For system("cls")

using namespace std;

class node {
private:
    string name;
    int age;
    float height;
public:
    node *next;
    static node *start_ptr; // 用static保证所有实例共享链表头指针

    node() : next(nullptr), age(0), height(0.0f) {} // 初始化成员变量

    void getInput() {
        node *temp = new node; // 用局部temp替代类成员,避免意外副作用
        cout << "Name: ";
        cin >> temp->name;
        cout << "Age: ";
        cin >> temp->age;
        cout << "Height: ";
        cin >> temp->height;
        cout << "\n";
        temp->next = nullptr;

        if (start_ptr == nullptr) {
            start_ptr = temp;
        } else {
            node *temp2 = start_ptr;
            while (temp2->next != nullptr) {
                temp2 = temp2->next;
            }
            temp2->next = temp;
        }
    }

    void update() {
        if (start_ptr == nullptr) {
            cout << "No record to update!" << endl;
            return;
        }

        int updateAge;
        cout << "Please enter age: ";
        cin >> updateAge;

        node *current = start_ptr;
        // 遍历链表查找匹配年龄的节点
        while (current != nullptr && current->age != updateAge) {
            current = current->next;
        }

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

        int ch;
        do {
            cout << "What Information You Want To Update?" << endl;
            cout << "1. Name" << endl;
            cout << "2. Height" << endl;
            cin >> ch;

            switch(ch) {
                case 1: {
                    cout << "Enter New Name: ";
                    cin.ignore(); // 清除cin >> ch留下的换行符
                    getline(cin, current->name);
                    cout << "RECORD UPDATED !" << endl;
                    break;
                }
                case 2: {
                    cout << "Enter New Height: ";
                    cin >> current->height;
                    cout << "RECORD UPDATED !" << endl;
                    break;
                }
                default: {
                    cout << "Wrong Input! Please choose again: ";
                }
            }
        } while (ch < 1 || ch > 2); // 循环直到用户输入有效选项
    }

    // 保留你已有的其他函数(printList、delete_end_node等)
    void printList() {
        node *current = start_ptr;
        if (current == nullptr) {
            cout << "List is empty!" << endl;
            return;
        }
        while (current != nullptr) {
            cout << "Name: " << current->name << ", Age: " << current->age << ", Height: " << current->height << endl;
            current = current->next;
        }
    }
};

// 初始化静态成员start_ptr为nullptr
node* node::start_ptr = nullptr;

// 测试用的主函数示例
int main() {
    node list;
    list.getInput();
    list.getInput();
    list.printList();
    list.update();
    list.printList();
    return 0;
}

关键修改说明

  • 静态start_ptr: 将start_ptr设为静态成员,确保所有node实例共享同一个链表头指针,避免指针混乱。
  • 局部临时指针: 在getInput()中用局部temp替代类成员指针,避免意外修改全局状态。
  • 修复节点访问: 用current->age替代current->temp->age,正确访问每个节点的年龄属性。
  • 输入验证循环: 给菜单添加do-while循环,强制用户输入有效选项(1或2),而不是直接退出。
  • 成员初始化: 在node构造函数中添加成员初始化列表,避免未初始化变量的问题。
  • 移除冗余代码: 删除了无用的struct list和多余的类成员指针。

经过这些修改后,你的更新功能应该可以正常工作——你可以通过年龄查找记录,并修改其姓名或身高,程序也不会再崩溃了。

内容的提问来源于stack exchange,提问作者Aurora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:51:57