反转双向链表

时间:2014-01-06 07:02:29

标签: data-structures linked-list reverse doubly-linked-list

这是我的简单链接列表程序,它创建了一个双向链表,并且可以正常工作。

#include <iostream>
using namespace std;

typedef struct node {
  int data;
  node *next;
  node *prev;
}node;

void printList(node *temp);

int main()
{
    node *head;
    head = new node;
    head->prev = NULL;
    node *next = head;
    node *prev = head;
    node *temp = head;
    node *current = head;

    //creates 100 nodes, last one points to next
    for(int x = 0; x<100; x++)
    {
    temp->data = x;
    current = temp;
    temp = new node;
    current->next = temp;
    temp->prev = current;
    temp->next = NULL;
    }
    //=========================================

    printList(head);

    //=========== set everything to head ===========
    current = head;
    prev = head;

    //============= reverses linked list ============
    while(current->next != NULL)
    {   
    next = current->next;   //moves next pointer to next node
    current->prev = next;   //points current's previous to next node
    current = next;         //set current pointer to next node
    current->next = prev;   //set current's next to previous node
    prev = current;         //move prev node up to current
    }
    //================================================

    printList(head);
    cout<<"done";

    return 0;
}    

void printList(node *temp)
{
    while(temp->next != NULL)
    {
        cout<<temp->data<<'\n';
        temp = temp->next;
    }
}

一旦我添加了反向功能,它就会挂起。实际上,函数本身是可行的,但在IDE中,当我循环它时,它会打印出所有值,然后挂起(坐在那里闪烁光标)并且什么都不做。

解决方案:让它发挥作用。这就是我的功能最终的结果。

current = head;         //set current pointer to head
prev = head;            //set previous pointer to head


next = current->next;   //moves next pointer to next node
current->next = NULL;   //set the next of the header to NULL, because it will actually be the last
                        //node of reversed list.
current->prev = next;   //set previous of the header to the next node.

while(next != NULL)
{
current = next;
next = current->next;
current->prev = next;
current->next = prev;
prev = current;
}

2 个答案:

答案 0 :(得分:1)

您的反向算法基本上已经破解。

在第一次通过时:

current = head; //  Current is pointing at node 0, node0->next is 1 from before
prev = head; // Prev is pointing at node 0

next = current->next; //  next is pointing at 1
current->prev = next; //  node0->prev is pointing at 1
current = next;       //  current is pointing at 1
current->next = prev  //  node1->next is pointing at 0

然后下一遍

next = current->next //  read up there ^^^   node1->next is pointing at 0

...接下来回到节点0。

这不是你想要做的 - 它会让你重复循环节点1和零,而不是前进到节点2以及... ...

请注意,如果将此代码放入反向循环中,您可以轻松调试它:

cout<<"\nStarting iteration"
cout<<"\nNext is at" << next->data
cout<<"\nCurrent is at" << current->data
cout<<"\nCurrent->next is" << current->next->data

等...不需要很长时间打字,揭示所有:)

(可能你会把它减少为3而不是100)

我只是手动(纸上)完成了3个节点的步骤,以推断出这个答案......

答案 1 :(得分:0)

看看这个简单的解决方案..

Node* Reverse(Node* head)
{
Node * curr=head;
Node * prev=NULL,* nxt=NULL;

while(curr!=NULL)
    {
    nxt=curr->next;

    curr->next=prev;
    curr->prev=nxt;

    prev=curr;
    curr=nxt;
    }

return prev;
// Complete this function
// Do not write the main method. 
}