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

C++单链表remove方法运行出现segmentation fault错误求助

C++单链表段错误问题修复

核心问题点

  • 类型转换错误:所有将指针强制转为Object类型的写法非法,比如Node构造函数默认参数Node *n = (Object)NULL、指针判空时的ptr != (Object)NULL,错误的类型转换会导致指针地址异常,访问非法内存触发段错误。
  • remove方法逻辑缺陷:
    • 删除节点成功后未减少theSize的值(theSize--被注释),导致print方法遍历时会按原有长度访问已经释放的节点内存。
    • 删除头节点成功后直接return 0,不仅返回值错误,还跳过了计数更新逻辑。
  • 无效哨兵节点问题:init方法多余创建了无数据的哨兵节点,后续add逻辑直接替换链表头,哨兵节点会一直留在链表末尾,访问时会读到无效数据。
  • 模板通用性错误:contains方法参数写死为int x,不符合模板类的设计要求,且原拷贝构造函数存在传参类型错误、野指针访问问题。

修复后完整代码

#include <iostream>
using namespace std;

template <typename Object>
struct Node
{
    Object data;
    Node* next;
    Node(const Object &d = Object(), Node *n = nullptr) : data(d), next(n) {}
};

template <typename Object>
class singleList
{
public:
    singleList() { init(); }
    ~singleList() { eraseList(head); }
    // 原拷贝构造逻辑错误,暂注释,需要使用可另行实现深拷贝逻辑
    // singleList(const singleList &rhs)
    // {
    //     eraseList(head);
    //     init();
    //     *this = rhs;
    //     print();
    //     contains(head);
    // }

    void init()
    {
        theSize = 0;
        head = nullptr;
    }

    void eraseList(Node<Object> *h)
    {
        Node<Object> *ptr = h;
        Node<Object> *nextPtr;
        while (ptr != nullptr)
        {
            nextPtr = ptr->next;
            delete ptr;
            ptr = nextPtr;
        }
    }

    int size()
    {
        return theSize;
    }

    void print()
    {
        Node<Object> *current = head;
        for(int i=0; i < theSize; ++i){
            cout << current->data << " ";
            current = current->next;
        }
    }

    bool contains(const Object& x)
    {
        Node<Object> *current = head;
        for (int i = 0; i < theSize; ++i){
            if (current->data == x){
                return true;
            }
            current = current -> next;
        }
        return false;
    }

    bool add(Object x){
        if(!contains(x)){
            Node<Object> *new_node = new Node<Object>(x);
            new_node->next = head;
            head = new_node;
            theSize++;
            return true;
        }
        return false;
    }

    bool remove(const Object& x)
    {
        if(contains(x)){
            Node<Object> *temp = head;
            Node<Object> *prev = nullptr;

            if(temp != nullptr && temp ->data == x){
                head = temp->next;
                delete temp;
                theSize--;
                return true;
            }
            while(temp != nullptr && temp->data != x){
                prev = temp;
                temp = temp->next;
            }
            prev->next = temp->next;
            delete temp;
            theSize--;
            return true;
        }
        return false;
    }

private:
    Node<Object> *head;
    int theSize;
};

 int main()
 {
     singleList<int> *lst = new singleList<int>();
     lst->add(10);
     lst->add(12);
     lst->add(15);
     lst->add(6);
     lst->add(3);
     lst->add(8);
     lst->add(3);
     lst->add(18);
     lst->add(5);
     lst->add(15);

     cout << "The original linked list: ";
     lst->print();
     cout << endl;
     lst->remove(6);
     lst->remove(15);
     cout << "The updated linked list: ";
     lst->print();
     cout << endl;
     cout << "The number of node in the list: " << lst->size() << endl;

     delete lst;
     return 0;
 }

运行结果

The original linked list: 5 18 8 3 6 15 12 10 
The updated linked list: 5 18 8 3 12 10 
The number of node in the list: 6

与预期输出完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 04:27:04