File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ struct Node {
5
+ int data;
6
+ struct Node * next;
7
+ Node (int data)
8
+ {
9
+ this ->data = data;
10
+ next = NULL ;
11
+ }
12
+ };
13
+
14
+ struct LinkedList {
15
+ Node* head;
16
+ LinkedList ()
17
+ {
18
+ head = NULL ;
19
+ }
20
+ void push (int data)
21
+ {
22
+ Node* temp = new Node (data);
23
+ temp->next = head;
24
+ head = temp;
25
+ }
26
+ void display ()
27
+ {
28
+ struct Node * temp = head;
29
+ while (temp != NULL )
30
+ {
31
+ cout << temp->data << " " ;
32
+ temp = temp->next ;
33
+ }
34
+ cout<<endl;
35
+ }
36
+ };
37
+
38
+ int main ()
39
+ {
40
+ int n, data;
41
+ LinkedList ll;
42
+ cout<<" Enter the number of elements to be added in Linked List: " ;
43
+ cin>>n;
44
+ for (int i=0 ;i<n;i++)
45
+ {
46
+ cout<<" Enter the data item " <<i+1 <<" : " ;
47
+ cin>>data;
48
+ ll.push (data);
49
+ }
50
+ cout << " The Linked List is:\n " ;
51
+ ll.display ();
52
+ return 0 ;
53
+ }
You can’t perform that action at this time.
0 commit comments