sports528
11-20-2007, 01:36 AM
I am trying to reverse a double linked list, but keep getting seg faults, infinite loops, or just my first term (after being reversed). I am trying to switch the next and prev pointers between two nodes, then switch head and tail.
Can someone help? all other functions work (size(), etc.)
My files/code is as follows:
in list.cpp
List::List() {
head = NULL;
tail = NULL;
}
void List::reverse(void) {
Node *cur = head;
Node *temp,*temp2,*temp3;
List *ltemp = new List();
if (size() > 1) {
// for (int i = 0;i < size()-1;i++) {
while (cur->next != NULL) {
temp=cur->next;
temp2 = cur->next->prev;
temp3 = temp;
temp = temp2;
temp2 = temp3;
//cur->next->prev=temp;
//cur->next = temp2;
cur = cur->next;
}
//switching last node
cur=tail;
cur->next=head;
cur->prev=NULL;
temp = head;
head = tail;
tail = temp;
}
}
in node.cpp
#include "node.h"
#define NULL 0
extern Node *next;
extern Node *prev;
Node::Node(int num) {
data = num;
next = NULL;
prev = NULL;
}
Can someone help? all other functions work (size(), etc.)
My files/code is as follows:
in list.cpp
List::List() {
head = NULL;
tail = NULL;
}
void List::reverse(void) {
Node *cur = head;
Node *temp,*temp2,*temp3;
List *ltemp = new List();
if (size() > 1) {
// for (int i = 0;i < size()-1;i++) {
while (cur->next != NULL) {
temp=cur->next;
temp2 = cur->next->prev;
temp3 = temp;
temp = temp2;
temp2 = temp3;
//cur->next->prev=temp;
//cur->next = temp2;
cur = cur->next;
}
//switching last node
cur=tail;
cur->next=head;
cur->prev=NULL;
temp = head;
head = tail;
tail = temp;
}
}
in node.cpp
#include "node.h"
#define NULL 0
extern Node *next;
extern Node *prev;
Node::Node(int num) {
data = num;
next = NULL;
prev = NULL;
}