1+ #include < iostream>
2+ using namespace std ;
3+
4+ template <class temp >
5+ class Node
6+ {
7+ // -------------------------------------------Creation of Node-------------------------------------------
8+ public :
9+ temp data;
10+ Node *next;
11+ };
12+
13+ template <class temp >
14+ class Linkedlist
15+ {
16+ public :
17+ Node<temp> *start, *ptr;
18+
19+ Linkedlist ()
20+ {
21+ start = NULL ;
22+ }
23+
24+ void insertFront ()
25+ {
26+ // -------------------------------------------Insertion in Front-------------------------------------------
27+ Node<temp> *newnode = new Node<temp>();
28+ cout<<" Enter the element : " ;
29+ cin>>newnode->data ;
30+ newnode->next = NULL ;
31+ if (start == NULL )
32+ {
33+ start = newnode;
34+ }
35+ else
36+ {
37+ newnode->next = start;
38+ start = newnode;
39+ }
40+ }
41+
42+ void insertEnd ()
43+ {
44+ // -------------------------------------------Insertion in End-------------------------------------------
45+ Node<temp> *newnode = new Node<temp>();
46+ cout<<" Enter the element : " ;
47+ cin>>newnode->data ;
48+ newnode->next = NULL ;
49+ if (start == NULL )
50+ {
51+ start = newnode;
52+ }
53+ else
54+ {
55+ ptr = start;
56+ while (ptr->next !=NULL )
57+ {
58+ ptr = ptr->next ;
59+ }
60+ ptr->next = newnode;
61+ }
62+ }
63+
64+ void insertMiddle ()
65+ {
66+ // -------------------------------------------Insertion in Middle-------------------------------------------
67+ int index, i=0 ;
68+ bool flag = false ;
69+ Node<temp> *newnode = new Node<temp>();
70+ ptr = start;
71+ if (start==NULL )
72+ {
73+ cout<<" Enter the element : " ;
74+ cin>>newnode->data ;
75+ newnode->next = NULL ;
76+ start = newnode;
77+ }
78+ else
79+ {
80+ cout<<" Enter the index at which the element has to be inserted : " ;
81+ cin>>index;
82+ while (ptr->next !=NULL and flag==false )
83+ {
84+ if (i==index)
85+ {
86+ flag = true ;
87+ break ;
88+ }
89+ ptr = ptr->next ;
90+ i++;
91+ }
92+ if (flag==true )
93+ {
94+ cout<<" Enter the element : " ;
95+ cin>>newnode->data ;
96+ newnode->next = ptr->next ;
97+ ptr->next = newnode;
98+ }
99+ else
100+ {
101+ cout<<" Invalid index" <<endl;
102+ }
103+ }
104+
105+
106+ }
107+
108+ void printList ()
109+ {
110+ // -------------------------------------------Printing Nodes-------------------------------------------
111+ cout<<" The nodes in the list are : " ;
112+ ptr = start;
113+ while (ptr!=NULL )
114+ {
115+ cout<<ptr->data <<' \t ' ;
116+ ptr = ptr->next ;
117+ }
118+ cout<<endl;
119+ }
120+
121+ void deleteFront ()
122+ {
123+ // -------------------------------------------Deletion in Front-------------------------------------------
124+ ptr = start->next ;
125+ start->next = NULL ;
126+ start = ptr;
127+ }
128+
129+ void deleteMiddle ()
130+ {
131+ // -------------------------------------------Deletion in middle-------------------------------------------
132+ int index, i=0 ;
133+ cout<<" Enter the index at which the element has to be deleted : " ;
134+ cin>>index;
135+ ptr = start;
136+ while (ptr!=NULL && i!=index)
137+ {
138+ ptr = ptr->next ;
139+ i += 1 ;
140+ }
141+ if (i==index)
142+ {
143+ Node<temp> *newnode;
144+ newnode = ptr->next ;
145+ ptr->next = newnode->next ;
146+ newnode->next = NULL ;
147+ }
148+ else
149+ {
150+ cout<<" Invalid Index" <<endl;
151+ }
152+ }
153+
154+ void deleteEnd ()
155+ {
156+ // -------------------------------------------Deletion in End-------------------------------------------
157+ ptr = start;
158+ while (ptr->next ->next !=NULL )
159+ {
160+ ptr = ptr->next ;
161+ }
162+ ptr->next = NULL ;
163+ }
164+
165+ void search ()
166+ {
167+ // -------------------------------------------Searching-------------------------------------------
168+ cout<<" Enter the element to be searched : " ;
169+ temp t;
170+ cin>>t;
171+ ptr = start;
172+ bool flag = false ;
173+ while (ptr!=NULL )
174+ {
175+ if (ptr->data ==t)
176+ {
177+ flag = true ;
178+ break ;
179+ }
180+ ptr = ptr->next ;
181+ }
182+ if (flag)
183+ {
184+ cout<<" The element is found." <<endl;
185+ }
186+ else
187+ {
188+ cout<<" The element is not found." <<endl;
189+ }
190+ }
191+ };
192+
193+ void printOptions ()
194+ {
195+ cout<<" The avalible options are : " <<endl;
196+ cout<<" 1) Insert a node in the middle" <<endl;
197+ cout<<" 2) Insert a node in the front" <<endl;
198+ cout<<" 3) Insert a node in the end" <<endl;
199+ cout<<" 4) Delete a node in the middle" <<endl;
200+ cout<<" 5) Delete a node in the front" <<endl;
201+ cout<<" 6) Delete a node in the end" <<endl;
202+ cout<<" 7) Print the nodes." <<endl;
203+ cout<<" 8) Search for an element in the list." <<endl;
204+ cout<<" 9) Exit" <<endl;
205+ }
206+
207+
208+ int main ()
209+ {
210+ Linkedlist<int > list;
211+ int cho;
212+ do
213+ {
214+ printOptions ();
215+ cout<<" Enter your option : " ;
216+ cin>>cho;
217+ switch (cho)
218+ {
219+ case 1 :
220+ list.insertMiddle ();
221+ break ;
222+ case 2 :
223+ list.insertFront ();
224+ break ;
225+ case 3 :
226+ list.insertEnd ();
227+ break ;
228+ case 4 :
229+ list.deleteMiddle ();
230+ break ;
231+ case 5 :
232+ list.deleteFront ();
233+ break ;
234+ case 6 :
235+ list.deleteEnd ();
236+ break ;
237+ case 7 :
238+ list.printList ();
239+ break ;
240+ case 8 :
241+ list.search ();
242+ break ;
243+ case 9 :
244+ cout<<" End of program." <<endl;
245+ break ;
246+ default :
247+ cout<<" Invalid Option" <<endl;
248+ }
249+ cout<<" -------------------------------------------------------------------------------------------------" <<endl;
250+ } while (cho!=9 );
251+ }
0 commit comments