help with linked list

why does it print the adress not the list

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


struct nodeType
{
	int data;
	nodeType *link;
};
int main()
{
	
	
	
	nodeType *first;
	nodeType *last;
	nodeType *newNode;
	int num;

	first = NULL;
	last = NULL;

	
	
	
	for (int i = 0; i<3; i++)
	{
		cout << "Enter number :";
		cin >> num;
		newNode = new nodeType;         
		newNode->data = num;           
		newNode->link = NULL;           
		if (first == NULL)              
		{                               
			first = newNode;
			last = newNode;
		}
		else                            
		{                               
			last->link = newNode;       
			last = newNode;
		}
	}
	
	
	
	cout << first << endl;
	system("PAUSE");
	return(0);
}
That's because first is a pointer of type nodeType. If you want it to print out the data in the list try:

cout << first->data << endl;

samething if you want to print the last element try

cout << last->data << endl;


good luck.
Last edited on
thanks but what if i wanna print all the data i mean not only the last or the first the assigment was to create a linked list
Topic archived. No new replies allowed.