File tree Expand file tree Collapse file tree 1 file changed +150
-0
lines changed
Data Structures/Linked Lists/Singly Linked List Expand file tree Collapse file tree 1 file changed +150
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+
3
+ */
4
+
5
+ #include < iostream>
6
+ #include < bits/stdc++.h>
7
+ using namespace std ;
8
+
9
+ struct Node
10
+ {
11
+ int data;
12
+ Node *next;
13
+
14
+ };
15
+
16
+ // inserts to the list at the beginning
17
+
18
+ Node* insert_to_list (Node* head){
19
+
20
+ cout << " enter the number to be inserted in beginning : " ;
21
+
22
+ int number;
23
+
24
+ cin >> number;
25
+
26
+ Node* temp = new Node ();
27
+
28
+ temp -> data = number;
29
+
30
+ temp -> next = head;
31
+
32
+ head = temp;
33
+
34
+ return head;
35
+
36
+ }
37
+
38
+ // deletes the first node from list
39
+
40
+ Node* delete_from_list (Node* head){
41
+
42
+ // cout << "enter the number to be removed from list : ";
43
+
44
+ // int number;
45
+
46
+ // cin >> number;
47
+
48
+ Node* temp = new Node ();
49
+
50
+ temp = head;
51
+
52
+ head = head -> next;
53
+
54
+ delete temp;
55
+
56
+ return head;
57
+
58
+ }
59
+
60
+ // prints list
61
+
62
+ void print_list (Node* n){
63
+
64
+ while (n != NULL )
65
+ {
66
+ cout << n -> data << " " ;
67
+ n = n -> next;
68
+ }
69
+ cout << endl;
70
+
71
+ }
72
+
73
+
74
+ int main ()
75
+ {
76
+
77
+ Node* head = new Node ();
78
+ Node* second = new Node ();
79
+ Node* third = new Node ();
80
+
81
+ head -> data = 1 ;
82
+ head -> next = second;
83
+
84
+ second -> data = 2 ;
85
+ second -> next = third;
86
+
87
+ third -> data = 3 ;
88
+ third -> next = NULL ;
89
+
90
+ bool quit = false ;
91
+ int choice;
92
+
93
+ while (!quit){
94
+
95
+ cout << " 1. add to list begninnig" << endl
96
+ << " 2. delete first node from list" << endl
97
+ << " 3. print list" << endl
98
+ << " 4. quit" << endl;
99
+
100
+ cin >> choice;
101
+
102
+ switch (choice){
103
+
104
+ case 1 :
105
+ {
106
+ Node* temp = insert_to_list (head);
107
+ head = temp;
108
+ print_list (temp);
109
+ break ;
110
+ }
111
+
112
+ case 2 :
113
+ {
114
+ Node* temp = delete_from_list (head);
115
+ head = temp;
116
+ print_list (temp);
117
+ break ;
118
+ }
119
+
120
+ case 3 :
121
+ {
122
+ print_list (head);
123
+ break ;
124
+ }
125
+
126
+ case 4 :
127
+ {
128
+ quit = true ;
129
+ break ;
130
+ }
131
+
132
+ default :{
133
+ cout << " That is not a valid input, quitting program" ;
134
+ quit = true ;
135
+
136
+ }
137
+ }
138
+ }
139
+
140
+ // Node* temp = insert_node(head);
141
+
142
+ // print_list(temp);
143
+
144
+
145
+ // Node* temp = delete_node(head);
146
+
147
+ // print_list(temp);
148
+
149
+ return 0 ;
150
+ }
You can’t perform that action at this time.
0 commit comments