mardi 26 mai 2020

After I delete one node, anothor node also be deleted

I am doing some list excercise, and try to write a mergesort algorithm, but after I delete a node, which has been sorted in list, another two node also been deleted, I don't know why. And here is my code. I run the code below try to delete q, put it before p, but after I did this, the Predecessor and the successor has also being deleted.

insertBefore(p, L.remove((q = q->succ)->pred)); 

here is remove function:

 T  List<T>::remove(ListNodePosi(T)&p)//remove node
    {
        T e = p->data;
        p->pred->succ = p->succ;
        p->succ->pred = p->pred;
        delete p;
        _size--;
        return e;
    }

Here is insert Before:

ListNodePosi(T)  List<T>::insertBefore(ListNodePosi(T)&p, T const&e)//作为前驱插入
{
    p->insertAsPred(e);
    return p->pred;
}

Here is insert as predcessor:

 ListNodePosi(T) ListNode<T>::insertAsPred(T const&e)
    {
        ListNodePosi(T) x = new ListNode<T>(e, pred, this);
        pred->succ = x;
        pred = x;
        return x;
    }

Aucun commentaire:

Enregistrer un commentaire