删除节点时出现分段错误

时间:2015-05-12 08:07:43

标签: c++

我有一个只运行基本链表的C ++代码 - 添加新节点,删除节点并查看列表

//UNIX Environment
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#define clear() printf("\033[H\033[J")
using namespace std;

struct node
{
    int val;
    node *next;
};

struct node* head = NULL;

void add_node(struct node *hea)
{
    char f;
    int val;
    cout<<"Enter value : ";
    cin>>val;
    if(head==NULL)
    {
        head = new(struct node);
        head->val = val;
        head->next = NULL;
    }
    else
    {
        node *traverser = hea;
        node *traverser_prev = NULL;
        while(traverser!=NULL)
        {
            traverser_prev = traverser;
            traverser=traverser->next;
        }
        traverser = new(struct node);
        traverser->val = val;
        traverser->next = NULL;
        traverser_prev->next=traverser;
    }
    cout<<"\nEnter Y to return : ";
    cin>>f;
    while(f!='Y')
    {
        cout<<"Wrong entry.Enter again : ";
        cin>>f;
    }
    return;
}

void view_list(struct node *hea)
{
    char f;
    node *traverser = hea;
    while(traverser!=NULL)
    {
        cout<<traverser->val<<" ";
        traverser=traverser->next;
    }
    cout<<"\nEnter Y to return : ";
    cin>>f;
    while(f!='Y')
    {
        cout<<"Wrong entry.Enter again : ";
        cin>>f;
    }
    return;
}

void delete_node(node *hea)
{
    char f;
    int del_value;
    cout<<"Enter value to be deleted :";
    cin>>del_value;

    node *traverser = hea;
    cout<<"OK1\n";
    node* traverser_prev = NULL;
    cout<<"OK2\n";

    while(traverser!=NULL)
    {
        if(traverser->val==del_value)
        {
            if(traverser==hea)
            {
                hea=traverser->next;
                cout<<"Here cond1\n";
                delete traverser;
            }
            else
            {
                traverser_prev->next = traverser->next;
                cout<<"Here cond2\n";
            }
        }
        else
        {
            traverser_prev = traverser;
            traverser = traverser->next;
            cout<<"Here cond3\n";
        }
    }
    cout<<"\nEnter Y to return : ";
    cin>>f;
    while(f!='Y')
    {
        cout<<"Wrong entry.Enter again : ";
        cin>>f;
    }
    return;
}
int main(int argc, char* argv[])
{
    while(1)
    {
        clear();
        int choice;
        cout<<"POINTER BASICS\n";
        cout<<"1. Add new element\n";
        cout<<"2. View elements\n";
        cout<<"3. Delete element\n";
        cout<<"4. Exit\n";
        cout<<"Enter choice: ";
        cin>>choice;
        switch(choice)
        {
            case 1: add_node(head);
                    break;
            case 2: view_list(head);
                    break;
            case 3: delete_node(head);
                    break;
            default:break;
        }
        if(choice==4)
            break;
    }
}

但是,每当我尝试删除节点时,它会抛出核心转储的分段错误错误。错误的原因是什么?

2 个答案:

答案 0 :(得分:2)

一个问题是在delete_node函数中你通过值传递列表头,这意味着指针被复制并且只在函数内部修改副本。

有两种解决方案:要么根本不使用参数,只使用全局变量,要么通过引用传递参数

void delete_node(node*& hea)

答案 1 :(得分:-1)

执行行

时是否可能

traverser_prev->next = traverser->next;

traverser_prev == NULL?