Doubly Linked List printing problem

I can't figure out how to write the display function. I want a function to print the array backwards and a function that prints it forward. But I'm unable to write even the simple one.

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
  #include <iostream>
using namespace std;

struct node
{
	node* prev;
	int data;
	node* next;
};

class dlinkedlist
{
	node* first; 
	node* last;  
public:
	dlinkedlist()
	{
		first = NULL;
		last = NULL;
	}

	void addItem(int d)
	{
		node* pNode = new node;
		pNode->data = d;
		pNode->prev = first;
		pNode->next = last;
		if(pNode->prev != NULL)
		{
			pNode->prev = first;
			pNode->next = NULL;
		}
		last = pNode;
	}

	void display()
	{
		node* current = first;
		for(current = first; current != NULL; current = current->next)
		{
			cout << current->data << endl;
		}
	}
};

int main()
{
	int nValues;
	int values;

	dlinkedlist mydLL;

	cout << "How many values: ";
	cin  >> nValues;
	cout << "Enter " << nValues << " values:\n";

	for(;nValues != 0; nValues--)
	{
		cin  >> values;
		mydLL.addItem(values);
	}

	cout << endl;
	mydLL.display();

	cout << endl;
	system("pause");
}
What's wrong with display()? It looks ok to me.
it displays the list in reverse order. What do i do to prevent that?
it displays the list in reverse order.
Perhaps the data is inserted incorrectly.

If addItem() is adding entries to the front, it should look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	void addItem(int d)
	{
		node* pNode = new node;
		pNode->data = d;

		if (first == NULL && last == NULL)
		{
			// insert first item
			pNode->next = last;
			pNode->prev = first;
			first = last = pNode;
		}
		else
		{
			pNode->next = first;
			pNode->prev = NULL;
			first = pNode;
		}
	}

Topic archived. No new replies allowed.