Trouble with inserting in Linked Lists.

I'm using a doubly linked list and templates and I'm having trouble with inserting a new node and printing it in reverse. I can print the list in reverse when I append a new node to it, but when I insert a new node it won't show up. When I print the list normally the nodes that I inserted are there. I may be missing something obvious, if someone could help me out I would be most thankful.

Here is my code for my insert function and reverse function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void insert(double val){
		ListNode<T> * newNode = new ListNode<T>;
		(*newNode).value = val;
		(*newNode).next = NULL;

		ListNode<T> * ptr = head, *prevNode = NULL;

		while(ptr != NULL && (*ptr).value < val){
			prevNode = ptr;
			ptr = (*ptr).next;
		}
		if(!prevNode) {
			head = newNode;
		}
		else {
			(*prevNode).next = newNode;
		}
		(*newNode).next = ptr;
	}

	void reverse(){
		ListNode<T> *ptr =head;
		 ptr = node;
    
		 while(ptr != NULL)
			{
                cout<<(*ptr).value<<"->";
                ptr = (*ptr).prev;
			} 
	 cout << "NULL" << endl;
}
Last edited on
Topic archived. No new replies allowed.