printing out doubly linked list

I am trying to have my linked list's ostream print out the previous node's location, then value of the current node, and then next node location but right now it will print a '0' instead of the actual location of the previous node. The value and next locations print fine though. What am I doing wrong?

linked list ostream
1
2
3
4
5
6
7
8
9
10
  template <typename T>
ostream &operator <<(ostream &os, const LinkedList<T> &linkedList) {
	Node<T> *p = linkedList.head;
	while (p) {
		cout << *p;
		p = p->next;
		if (p) cout << " -> ";
	}
	return os;
}


node ostream
1
2
3
4
friend std::ostream &operator <<(std::ostream &os, const Node<T> &node) {
		os << '[' << "#" << node.prev << " | " << node.data << " | #" << node.next << ']';
		return os;
	}


node members and constructor
1
2
3
Node(T data, Node<T> *next=NULL, Node<T> *prev=NULL) : data(data), next(next), prev(prev) {}
	T data;
	Node<T> *next, *prev;


and linked list class has a Node member named head.

output looks like this:
[#0 | 99 | #0x772140] -> [#0 | 50 | #0x722110} -> [#0 | 20 | #0}
Last edited on
Since you appear to be printing node.prev, I would have to guess that you didn't set node.prev correctly when you inserted the node or called your Node constructor.
you're right, i was focused on the ostreams when i was not setting the values correct during insertion, thanks for the perspective.
Topic archived. No new replies allowed.