How to show output of linked list after getting user input

Good day, I'm quite new to linked lists, and I would like to ask for help with completing my code, I'm not sure how I'm going to display the output of my program, the result I'm always getting is whatever the last number I input then a bunch of random numbers.

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

#include<iostream>
using namespace std;

struct Node{
	
    int data;
    Node *next;
	
};

void list(){

    int n;
    Node *node = NULL;
	
    cout << "How many data to be entered? ";
 
    cin >> n;        
            
         
	for(int z = 0; z < n; z++){
		
 		node = new Node;

 		cout << "input data ";

 		cin >> node->data;
    			
		}
}


int main(){
	


  list();


 	
  system("pause"); 	
 	
	
 	
  return 0;
 	
 }
 
You aren't linking the nodes together. It's this linking of nodes that makes a list. You have next as a member of Node but you aren't setting it. When you create a new node, next of the previous node is set to point to the new node. You also have a variable (usually called head) that points to the first node in the list. To display the list contents, you would start at head and follow the chain of nodes until no more nodes.

There's plenty of info about lists - both as threads on this form and elsewhere.
Topic archived. No new replies allowed.