1+ #include <stdio.h>
2+ #include <conio.h>
3+ #include <process.h>
4+
5+ struct node
6+ {
7+ int data ;
8+ struct node * next ;
9+ }* start = NULL ,* q ,* t ;
10+
11+ int main ()
12+ {
13+ int ch ;
14+ void insert_beg ();
15+ void insert_end ();
16+ int insert_pos ();
17+ void display ();
18+ void delete_beg ();
19+ void delete_end ();
20+ int delete_pos ();
21+
22+ while (1 )
23+ {
24+ printf ("\n\n---- Singly Linked List(SLL) Menu ----" );
25+ printf ("\n1.Insert\n2.Display\n3.Delete\n4.Exit\n\n" );
26+ printf ("Enter your choice(1-4):" );
27+ scanf ("%d" ,& ch );
28+
29+ switch (ch )
30+ {
31+ case 1 :
32+ printf ("\n---- Insert Menu ----" );
33+ printf ("\n1.Insert at beginning\n2.Insert at end\n3.Insert at specified position\n4.Exit" );
34+ printf ("\n\nEnter your choice(1-4):" );
35+ scanf ("%d" ,& ch );
36+
37+ switch (ch )
38+ {
39+ case 1 : insert_beg ();
40+ break ;
41+ case 2 : insert_end ();
42+ break ;
43+ case 3 : insert_pos ();
44+ break ;
45+ case 4 : exit (0 );
46+ default : printf ("Wrong Choice!!" );
47+ }
48+ break ;
49+
50+ case 2 : display ();
51+ break ;
52+
53+ case 3 : printf ("\n---- Delete Menu ----" );
54+ printf ("\n1.Delete from beginning\n2.Delete from end\n3.Delete from specified position\n4.Exit" );
55+ printf ("\n\nEnter your choice(1-4):" );
56+ scanf ("%d" ,& ch );
57+
58+ switch (ch )
59+ {
60+ case 1 : delete_beg ();
61+ break ;
62+ case 2 : delete_end ();
63+ break ;
64+ case 3 : delete_pos ();
65+ break ;
66+ case 4 : exit (0 );
67+ default : printf ("Wrong Choice!!" );
68+ }
69+ break ;
70+ case 4 : exit (0 );
71+ default : printf ("Wrong Choice!!" );
72+ }
73+ }
74+ return 0 ;
75+ }
76+
77+ void insert_beg ()
78+ {
79+ int num ;
80+ t = (struct node * )malloc (sizeof (struct node ));
81+ printf ("Enter data:" );
82+ scanf ("%d" ,& num );
83+ t -> data = num ;
84+
85+ if (start == NULL ) //If list is empty
86+ {
87+ t -> next = NULL ;
88+ start = t ;
89+ }
90+ else
91+ {
92+ t -> next = start ;
93+ start = t ;
94+ }
95+ }
96+
97+ void insert_end ()
98+ {
99+ int num ;
100+ t = (struct node * )malloc (sizeof (struct node ));
101+ printf ("Enter data:" );
102+ scanf ("%d" ,& num );
103+ t -> data = num ;
104+ t -> next = NULL ;
105+
106+ if (start == NULL ) //If list is empty
107+ {
108+ start = t ;
109+ }
110+ else
111+ {
112+ q = start ;
113+ while (q -> next != NULL )
114+ q = q -> next ;
115+ q -> next = t ;
116+ }
117+ }
118+
119+ int insert_pos ()
120+ {
121+ int pos ,i ,num ;
122+ if (start == NULL )
123+ {
124+ printf ("List is empty!!" );
125+ return 0 ;
126+ }
127+
128+ t = (struct node * )malloc (sizeof (struct node ));
129+ printf ("Enter data:" );
130+ scanf ("%d" ,& num );
131+ printf ("Enter position to insert:" );
132+ scanf ("%d" ,& pos );
133+ t -> data = num ;
134+
135+ q = start ;
136+ for (i = 1 ;i < pos - 1 ;i ++ )
137+ {
138+ if (q -> next == NULL )
139+ {
140+ printf ("There are less elements!!" );
141+ return 0 ;
142+ }
143+
144+ q = q -> next ;
145+ }
146+
147+ t -> next = q -> next ;
148+ q -> next = t ;
149+ return 0 ;
150+ }
151+
152+ void display ()
153+ {
154+ if (start == NULL )
155+ {
156+ printf ("List is empty!!" );
157+ }
158+ else
159+ {
160+ q = start ;
161+ printf ("The linked list is:\n" );
162+ while (q != NULL )
163+ {
164+ printf ("%d->" ,q -> data );
165+ q = q -> next ;
166+ }
167+ }
168+ }
169+
170+ void delete_beg ()
171+ {
172+ if (start == NULL )
173+ {
174+ printf ("The list is empty!!" );
175+ }
176+ else
177+ {
178+ q = start ;
179+ start = start -> next ;
180+ printf ("Deleted element is %d" ,q -> data );
181+ free (q );
182+ }
183+ }
184+
185+ void delete_end ()
186+ {
187+ if (start == NULL )
188+ {
189+ printf ("The list is empty!!" );
190+ }
191+ else
192+ {
193+ q = start ;
194+ while (q -> next -> next != NULL )
195+ q = q -> next ;
196+
197+ t = q -> next ;
198+ q -> next = NULL ;
199+ printf ("Deleted element is %d" ,t -> data );
200+ free (t );
201+ }
202+ }
203+
204+ int delete_pos ()
205+ {
206+ int pos ,i ;
207+
208+ if (start == NULL )
209+ {
210+ printf ("List is empty!!" );
211+ return 0 ;
212+ }
213+
214+ printf ("Enter position to delete:" );
215+ scanf ("%d" ,& pos );
216+
217+ q = start ;
218+ for (i = 1 ;i < pos - 1 ;i ++ )
219+ {
220+ if (q -> next == NULL )
221+ {
222+ printf ("There are less elements!!" );
223+ return 0 ;
224+ }
225+ q = q -> next ;
226+ }
227+
228+ t = q -> next ;
229+ q -> next = t -> next ;
230+ printf ("Deleted element is %d" ,t -> data );
231+ free (t );
232+
233+ return 0 ;
234+ }
0 commit comments