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

Swap first 2 nodes only in a doubly linked list

本文关键字:2个 节点 列表 交换 链接      更新时间:2023-10-16

在反向函数中,我试图只交换双链表中的前两个节点,而不交换数据。它运行,但不会交换列表中的前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 isn" << a << endl;
    reverse(a);
    cout << "AFTER a isn" << a << endl;
    cleanUp(a);
    a = NULL;
    addFront(a, 10);
    cout << "nONE ELEMENT LIST CASE:n";
    cout << "BEFORE a isn" << a << endl;
    reverse(a);
    cout << "AFTER a isn" << a << endl;
    cleanUp(a);
    a = NULL;
    addFront(a, 30);
    addFront(a, 20);
    cout << "nTWO ELEMENT LIST CASE:n";
    cout << "BEFORE a isn" << a << endl;
    reverse(a);
    cout << "AFTER a isn" << a << endl;
    cleanUp(a);
    a = NULL;
    addFront(a, 60);
    addFront(a, 50);
    addFront(a, 40);
    cout << "nTHREE ELEMENT LIST CASE:n";
    cout << "BEFORE a isn" << a << endl;
    reverse(a);
    cout << "AFTER a isn" << 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 isn" << a << endl;
    reverse(a);
    cout << "AFTER a isn" << 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