Help with doubly linked list ordering


Hi,

This is not the typical " I can't reverse a doubly linked list " post.

It actually does reverse the order. But I have one issue.

Let's say the list is:

A
B
C
D

When it prints it in reverse order, it displays:

D
C
B
A


But when it prints it in reverse again, it reversing again back to normal:

A
B
C
D


And if I do reverse again, it's back to:

D
C
B
A




My question is, is this how a doubly linked list is supposed to work?

Thanks

BTW, here's the code to my reverse method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void linkedList::reverse() 
{
	node* ptr = top;

	while (ptr != NULL) {
		node *tmp = ptr->next;
		ptr->next = ptr->prev;
		ptr->prev = tmp;

		if (tmp == NULL) {
			bottom = top;
			top = ptr;
		}

		ptr = tmp;
	}
}
Last edited on
is this how a doubly linked list is supposed to work?

Why wouldn't it work that way? Why would you expect a method called "reverse" to do anything other than reverse the order of the list?
Last edited on
Ok thanks, I was just asking to make sure.
Topic archived. No new replies allowed.