doubly Linked list print

I have no idea why this code doesn't print backwards.
If I create list with these numbers 12345. normal forwards function prints(12345)
while bakwards one prints(4321)
Excuse different language please.(this isn't full code)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  void create (node *&head, node *&temp, node *&tail)
{
	int sk;
	cout << "Irasykite norima skaiciu " << endl;
	cin >> sk;
	temp = new node;

	
	temp->data = sk;
	temp->next = NULL;

	if (head == NULL)
	{
		temp->prev = NULL;
		head = temp;
	}
	else
	{
		tail = head;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
			tail->next = temp;
			temp->prev = tail;
			
		
	}

}


void print(node *&head, node *&temp, node *&tail)
{
	if (head == NULL)
	{
		cout << "Sarasas tuscias" << endl;
		return;
	}

	
		temp = head;
		while (temp != NULL)
		{
			cout << temp->data << endl;
			temp = temp->next;

		}
	
}

void print_bakcwards(node *&head, node *&temp, node *&tail)
{
	if (head == NULL)
	{
		cout << "Sarasas tuscias" << endl;
		return;
	}

	
		
	temp = tail;
		while (temp != NULL)
		{
			
			
				cout << temp->data << endl;
				temp = temp->prev;
			
		}
	
}
You don't update the tail properly in the create().

If the list is empty, where does tail point to after insertion?
If the list is not empty, where does tail point to after insertion?
So I should delete tail = head; and switch it to something else?
sry I'm not getting it...
Look at your create() when head==NULL.
What does happen there?
1
2
temp = ... // a node is created and its values set
head = temp;

What did you do to the tail? Nothing.

If you call print_bakcwards() right after that first create(), what is the value of tail?
Should it not point to that one and only node of the list?

Similarly, if the list has items, your create:
1
2
3
// 1. make list point to the last node
// 2.
tail->next = temp;

The tail points to second to last node after the create().
Should it not point to the last node of the list?
Doesn't tail->next=temp; point to last node?
should I write tail=temp in create() if part?
How could tail point to the last node, if there is already node tail->next, which by definition is after the tail.

Yes, you have to set the tail to point to the last. If temp points to the last node, then tail=temp; is what you should do.
Topic archived. No new replies allowed.