列表的自定义交换功能导致分段错误

时间:2019-05-19 13:23:39

标签: c++ list segmentation-fault swap

我正在尝试为列表编写交换函数,但是一行显示了细分错误,我不知道为什么。

我试图杀死自己,但那没有用

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


node* swapnode(node* head, node* aprev, node* a, node* bprev, node* b)
{
    if(a == b) return head;
    if(a == head)
    {
        node* temp = a->next;
        a->next = b->next;
        b->next = temp;
        bprev->next = a;
        head = b;
        return head;
    }
    if(b == head)
    {
        node* temp = b->next;
        b->next = a->next;
        a->next = temp;
        aprev->next = b;
        head = a;
        return head;
    }
    node* temp;
    temp = a->next;
    a->next = b->next;
    b->next = temp;
    bprev->next = a;
    aprev->next = b;
    return head;

}

看起来像a-> next = b-> next这行给我带来了细分错误。

0 个答案:

没有答案
相关问题