仅交换双向链表中的前 2 个节点

Swap first 2 nodes only in a doubly linked list

在反向函数中,我尝试仅交换双向链表中的前 2 个节点而不交换数据。它运行但不交换列表中的前 2 个节点。有人可以指出我做错了什么的正确方向吗?

 struct node {
    int data;
    node * p;                //  FORWARD LINK
    node * rp;               //  REVERSE LINK
 };

 ostream & operator<<(ostream &, const node *);  
 void addFront(node * & start, int);             
 void cleanUp(node *);                          
 void reverse(node * &);              

 void main()
 {

    node * a = NULL;
    cout << "EMPTY LIST CASE:\n";
    cout << "BEFORE a is\n" << a << endl;
    reverse(a);
    cout << "AFTER a is\n" << a << endl;

    cleanUp(a);
    a = NULL;
    addFront(a, 10);
    cout << "\nONE ELEMENT LIST CASE:\n";
    cout << "BEFORE a is\n" << a << endl;
    reverse(a);
    cout << "AFTER a is\n" << a << endl;

    cleanUp(a);
    a = NULL;
    addFront(a, 30);
    addFront(a, 20);
    cout << "\nTWO ELEMENT LIST CASE:\n";
    cout << "BEFORE a is\n" << a << endl;
    reverse(a);
    cout << "AFTER a is\n" << a << endl;

    cleanUp(a);
    a = NULL;
    addFront(a, 60);
    addFront(a, 50);
    addFront(a, 40);
    cout << "\nTHREE ELEMENT LIST CASE:\n";
    cout << "BEFORE a is\n" << a << endl;
    reverse(a);
    cout << "AFTER a is\n" << a << endl;

    cleanUp(a);
    a = NULL;
    addFront(a, 400);
    addFront(a, 300);
    addFront(a, 200);
    addFront(a, 100);
    cout << "\nFOUR ELEMENT LIST CASE:\n";
    cout << "BEFORE a is\n" << a << endl;
    reverse(a);
    cout << "AFTER a is\n" << a << endl;

    cleanUp(a);
 }


 void reverse(node * & s)   
 {
    node * n1 = s;
    node * n2 = s;

    if (n1 == NULL) return;

    node * temp = new node;

    temp->rp = n1->rp;
    temp->p = n1->p;

    n1->rp = n2->rp;
    n1->p = n2->p;

    n2->rp = temp->rp;
    n2->p = temp->p;

    if (n1->p != NULL)
         n1->p->rp = n1;
    if (n1->rp != NULL)
         n1->rp->p = n1;
    if (n2->p != NULL)
         n2->p->rp = n2;
    if (n2->rp != NULL)
         n2->rp->p = n2;

    delete temp;
 }


 void addFront(node * & start, int x)
 {
    node * t = new node;
    t->data = x;
    if (start != NULL)
    {
        t->p = start;
         t->rp = NULL;
        start->rp = t;
    }
    else
    {
         t->p = NULL;
         t->rp = NULL;
    }
    start = t;
 }

 void cleanUp(node * s)
 {
    node * walker, *prev;
    walker = s;
    while (walker != NULL)
    {
        prev = walker;
         walker = walker->p;
        delete prev;
    }
 }    

 ostream & operator<<(ostream & w, const node * s)
 {
    const node * walker = s;
    const node * trailer = walker;
    w << "Forward Print " << endl;
    if (s == NULL)
    {
        w << "EMPTY LIST";
    }
    else
    {
        while (walker != NULL)
        {
            w << walker->data << ' ';
            trailer = walker;
            walker = walker->p;
        }
    }

    w << endl << "Reverse Print " << endl;
    if (trailer == NULL)
    {
        w << "EMPTY LIST";
         return w;
    }

    while (trailer != NULL)
    {
         w << trailer->data << ' ';
        trailer = trailer->rp;
    }
    return w;
 }

更新了完整的程序。

这是更新后的 reverse

void reverse(node * & s)
{
    node * n1 = s;

    if (n1 == NULL) return;

    node * n2 = n1->p;
    if (n2 == NULL) return;

    node *temp;
    node *n3;

    n3 = n2->p;

    temp = n1->rp;

    n1->p = n3;
    n1->rp = n2;

    n2->rp = temp;
    n2->p = n1;

    if (n3 != NULL)
        n3->rp = n1;

    s = n2;
}

注意:即使 temp 必须保持您定义的状态,也无需分配和删除它。我会使用 node temp 并将 temp->blah 替换为 temp.blah