Вот моя простая программа со связанными списками, которая создает дважды связанный список, и он работает.Обратный двойной список
#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, когда я LOOP это, она печатает все значения, а затем просто зависает (сидит там с мигающим курсором) и ничего не делает.
Решение: Получил его на работу. Это то, чем моя функция оказалась.
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;
}
Вы вставили операторы печати в каждую интересную точку кода и проследили, что происходит? Поскольку вы используете IDE, вы прошли через код и определили, где именно в коде, что среда «просто зависает». Что значит «просто зависает»? – GreenAsJade
Я пошел вперед и добавил оператор печати в обратную функцию. Это то, что я получил. Есть идеи? http://ideone.com/nvDNK2 –