1+ /*
2+ TOPIC: Linked List - Taking Input
3+
4+ - Taking input from user
5+ - Taking input from file
6+ */
7+
8+ #include < iostream>
9+ using namespace std ;
10+
11+ class Node {
12+ public:
13+ int data;
14+ Node *next;
15+
16+ // constructor
17+ Node (int d)
18+ {
19+ data = d;
20+ next = NULL ;
21+ }
22+ };
23+
24+
25+ // pass a pointer by reference (because we want to make changes to the original head pointer)
26+ void insertAtHead (Node *&head, int d)
27+ {
28+ if (head == NULL )
29+ {
30+ head = new Node (d);
31+ return ;
32+ }
33+
34+ Node *n = new Node (d);
35+ n->next = head;
36+ head = n;
37+ }
38+
39+
40+
41+ // function to find length of linked list
42+ int length (Node*head)
43+ {
44+ int cnt = 0 ;
45+ while (head != NULL )
46+ {
47+ cnt++;
48+ head = head->next ;
49+ }
50+ return cnt;
51+ }
52+
53+
54+
55+ // function to insert data at last position of linked list
56+ void insertAtTail (Node*&head, int data)
57+ {
58+ // corner case
59+ if (head == NULL )
60+ {
61+ // Node *n = new Node(data);
62+ // head = n;
63+ head = new Node (data);
64+ return ;
65+ }
66+ Node *tail = head; // using temp pointer
67+ // Moving head towards tail
68+ while (tail->next != NULL )
69+ {
70+ tail = tail->next ;
71+ }
72+ // create & attach new node
73+ tail->next = new Node (data);
74+ }
75+
76+
77+
78+ // function to insert data at middle of linked list
79+ void insertAtMiddle (Node*&head, int data, int pos)
80+ {
81+ // corner case
82+ if (head==NULL or pos==0 )
83+ {
84+ insertAtHead (head, data);
85+ return ;
86+ }
87+ else if (pos >= length (head))
88+ {
89+ insertAtTail (head, data);
90+ return ;
91+ }
92+ // take pos-1 jumps
93+ int i=1 ;
94+ Node *temp = head;
95+ while (i < pos)
96+ {
97+ temp = temp->next ;
98+ i++;
99+ }
100+ // create a new node
101+ Node *n = new Node (data);
102+ n->next = temp->next ;
103+ temp->next = n;
104+ }
105+
106+ // pass by value (because we don't want to change the original head pointer)
107+ void print (Node *head)
108+ {
109+ while (head != NULL )
110+ {
111+ cout << head->data << " -> " ;
112+ head = head->next ;
113+ }
114+ cout << endl;
115+ }
116+
117+
118+
119+ // function to delete data at start of linked list
120+ void deleteAtHead (Node*&head)
121+ {
122+ if (head == NULL )
123+ {
124+ return ;
125+ }
126+ Node*temp = head->next ; // temp pointer is static. So it will be deleted at the end of function call
127+ delete head; // delete the Node whose address is stored in "head" pointer
128+ head = temp;
129+ }
130+
131+
132+
133+ // function to delete data at end of linked list
134+ void deleteAtTail (Node*&head)
135+ {
136+ if (head == NULL )
137+ {
138+ return ;
139+ }
140+ Node *tail = head;
141+ Node *prev;
142+ // moving towards the end of linked list
143+ while (tail->next != NULL )
144+ {
145+ prev = tail;
146+ tail = tail->next ;
147+ }
148+ delete tail; // delete the Node whose address is stored in "tail" pointer
149+ // delete prev->next; // prev->nex & tail are pointing towards same address
150+ prev->next = NULL ;
151+ }
152+
153+
154+
155+ // function to delete data at end of linked list
156+ void deleteAtMiddle (Node*&head, int pos)
157+ {
158+ if (head==NULL )
159+ {
160+ return ;
161+ }
162+ else if (pos >= length (head))
163+ {
164+ return ;
165+ }
166+ // jump towards pos
167+ Node *temp = head;
168+ Node *prev;
169+ int i = 1 ;
170+ while (i <= pos)
171+ {
172+ prev = temp;
173+ temp = temp->next ;
174+ i++;
175+ }
176+ prev->next = temp->next ;
177+ delete temp; // delete the Node whose address is stored in "temp" pointer
178+ }
179+
180+
181+ // Search Operation
182+ // Linear Search
183+ bool search (Node*head, int key)
184+ {
185+ Node*temp = head;
186+ while (temp != NULL )
187+ {
188+ if (temp->data == key)
189+ {
190+ return true ;
191+ }
192+ temp = temp->next ;
193+ }
194+ // if key not found
195+ return false ;
196+ }
197+
198+
199+ // Linear Search (recursively)
200+ bool searchRecursive (Node*head, int key)
201+ {
202+ // base case
203+ if (head == NULL )
204+ {
205+ return false ;
206+ }
207+ // rec case
208+ if (head->data == key)
209+ {
210+ return true ;
211+ }
212+ return searchRecursive (head->next , key);
213+ }
214+
215+
216+ // Linked List User Input
217+ void take_input (Node*&head)
218+ {
219+ //
220+ }
221+
222+ // Linked List User Input
223+ Node* take_input_2 ()
224+ {
225+ Node *head = NULL ;
226+
227+ int data;
228+ cin >> data;
229+
230+ while (data != -1 )
231+ {
232+ insertAtHead (head, data);
233+ // insertAtTail(head, data);
234+ cin >> data;
235+ }
236+ return head;
237+ /* NOTE: Since we are adding elements at "head" of linked list.
238+ So, we will be getting linked list in the reverse order.
239+ */
240+ }
241+
242+
243+ // Linked List User Input [When taking Input from a file]
244+ Node* take_input_file ()
245+ {
246+ Node *head = NULL ;
247+
248+ int data;
249+ // cin >> data;
250+
251+ while (cin >> data)
252+ // while(data != -1)
253+ {
254+ insertAtHead (head, data);
255+ // cin >> data;
256+ }
257+ return head;
258+ /* NOTE: Since we are adding elements at "head" of linked list.
259+ So, we will be getting linked list in the reverse order.
260+ */
261+ }
262+
263+
264+
265+ int main ()
266+ {
267+ // since we don't have linked list class, we have to keep the track of head pointer
268+ Node *head = NULL ; // to store address of the first node
269+
270+ /*
271+ insertAtHead(head,3);
272+ insertAtHead(head,9);
273+ insertAtHead(head,74);
274+ insertAtMiddle(head,44,3);
275+ insertAtTail(head,88);
276+ */
277+
278+ // take input of linked list from user
279+ cout << " Enter Elements [Press -1 to Exit]: " ;
280+ head = take_input_2 ();
281+
282+ // // take input from file "input.txt"
283+ // head = take_input_file();
284+
285+ cout << " Linked List: " ;
286+ print (head);
287+
288+ return 0 ;
289+ }
290+
291+
292+ /*
293+ OUTPUT:
294+
295+ Case 1: [Input from USER]
296+ Enter Elements [Press -1 to Exit] : 6 5 10 9 7 3 -1
297+ Linked List : 3 -> 7 -> 9 -> 10 -> 5 -> 6
298+
299+ Case 2: [Input from FILE] (i.e In Terminal: program_file.cpp < input.txt)
300+ Linked List : -1 -> 7 -> 19 -> 3 -> 10 -> 9 -> 5 -> 4
301+
302+ */
0 commit comments