Help with a doubly linked list

Hello everyone, it's been a while since i've been on here!

Anyways, I'm having one heck of a time with a doubly linked list; specifically reversing the linked list. Here's what I have

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

template<typename T> // TODO
void DLinkedList<T>::RevList()
{
Node<T>* Tfirst = first; // creates new node pointer type called Tfirst which is set to the first node 
Node<T>* Tlast = last; // creates new node pointer type called Tlast and sets to the last node
Node<T>* TempC = 0; // creates new node pointer type called TempC and sets its value to zero

if (Tfirst == Tlast){ // if first is equal to last
	return; // just return
	}
if (Tlast == NULL || Tfirst == NULL) { // if First or Last are NULL
	return; // just return



without posting all of the code; I'm able to pass 2 of my 4 cases (both are for instances of an empty node and one where the first forward is equal to the last backward

I just need some help understanding how to traverse the list and swap the info/values for each node.
an example would be if i have 5 nodes and each of their info is {1,2,3,4,5} I want to switch them to {5,4,3,2,1}

I hope I explain what I can without giving out my code. I'm not asking for handout, just some understanding
I just need some help understanding how to traverse the list and swap the info/values for each node.
You are trying to reverse a doublly threaded linked list, right? So, have you considered just swapping the links on each node, then swapping the head/tail pointers?
if i had heads and tails i would; the class doesn't have them... but i think im allowed to modify the class and add them.. and I have thought of that, but it gets darn confusing and i get lost easily
You managed to implement a linked list class without keeping travk of the first node?
From the code, I would guess that first/last is synonymous with head/tail.
You don't actually have to care. As long as you have functions to properly insert and remove nodes, you only need traverse the list from head to tail once, and rebuild the list by appending each head node to the end of the new list. Then update the class's first and last pointers.

BTW, it really does help to get out a piece of paper and a pencil (I prefer crayons with lots of colors) and draw what you want to happen.

Good luck!
Topic archived. No new replies allowed.