小编Pri*_*bar的帖子

链表代码中的分段错误,删除部分

我正在尝试编写链接列表中的插入和删除代码。这是我在单链表中基本插入和删除节点的代码。代码中没有错误,但终端上的输出显示分段错误。有人可以解释为什么我会出现分段错误吗?我做了什么改变来消除故障。我认为分段错误出现在删除部分。请帮忙。

// class is a type of user defined datatype
class Node {
    public:
    int data;
    Node* next;

    //constructor
    Node(int data) {
        this -> data = data;
        this -> next = NULL;
    }

    // destructor
    ~Node() {
        int value = this -> data;
        //memory free krr rhe hain
        if(this -> next != NULL){
            delete next;
            this -> next = NULL;
        }
        cout << "memory is free for node with data" << value << endl;
    }

};

void insertAtHead(Node* &head, int …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list segmentation-fault nodes

0
推荐指数
1
解决办法
230
查看次数

标签 统计

c++ ×1

linked-list ×1

nodes ×1

segmentation-fault ×1