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

时间:2015-10-31 18:50:29

标签: c++ nodes swap 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;
 }

更新了完整的程序。

1 个答案:

答案 0 :(得分:0)

这是一个更新的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