-
Notifications
You must be signed in to change notification settings - Fork 1
/
09_Singly Linked List.c
351 lines (315 loc) · 7.56 KB
/
09_Singly Linked List.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//Singly Linked List
#include<stdio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
};
struct node *start= NULL;
struct node *end=NULL ;
void create()
{
char choice;
struct node *temp,*ptr,*end;
do
{
printf("\nEnter the data to be inserted: ");
temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
end->next=temp;
end=temp;
printf("Do you want to add another node? Enter Y for Yes and N for No: ");
scanf("%s",&choice);
}while(choice=='Y' || choice=='y');
}
void insertafter()
{
int x;
struct node *temp,*ptr,*end;
if(start == NULL)
{
printf("\nCreate a Linked List.\n");
}
else
{
printf("\n Enter the value after which the node is to inserted : ");
scanf("%d",&x);
ptr=start;
while(ptr!=NULL && ptr->data!=x)s
ptr=ptr->next;
if(ptr==NULL)
printf("\n The node not found!\n");
else
{
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the value of node to be inserted : ");
scanf("%d",&temp->data);
temp->next=ptr->next;
ptr->next=temp;
printf("The node inserted Successfully\n");
}
}
}
void insertbefore()
{
int x;
struct node *temp,*ptr,*end;
if(start==NULL)
printf("\nCreate the Linked List.\n");
else
{
printf("\n Enter the value of node before which the data has to be inserted : ");
scanf("%d",&x);
ptr=start;
end=NULL;
while(ptr!=NULL && ptr->data!=x)
{
end=ptr;
ptr=ptr->next;
}
if(ptr==NULL)
printf("Node Not Found.\n");
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the value of node : ");
scanf("%d",&temp->data);
temp->next=end->next;
end->next=temp;
printf("Node Inserted successfully\n");
/*lamlmlmvd;,fmLMF LMLFMMF LLD LML DLM LMLDMLFADLF*//*ZVP,ZD,,,,*//*DZDZMOMBOM
}
}
}
void delete_value()
{
int x;
struct node *temp,*ptr,*end;
if(start == NULL)
printf("\n The List is Empty!");
else
{
printf("\nEnter the value to be deleted: ");
scanf("%d",&x);
ptr=start;
end=NULL;
while(ptr!= NULL && ptr->data!=x)
{
end=ptr;
ptr=ptr->next;
}
if(ptr==NULL)
printf("Node Not Found.\n");
else
{
if(ptr==start)
start=start->next;
else
end->next=ptr->next;
free(ptr);
printf("Node Deleted successfully\n");
}
}
}
void delete_position()
{
int p,i;
struct node *temp,*ptr;
if(start==NULL)
printf("\n The List is Empty.\n");
else
{
printf("\n Enter the position of the node to be deleted: ");
scanf("%d",&p);
if(p==0)
{
ptr=start;
start=start->next;
printf("The deleted element is %d\n",ptr->data);
free(ptr);
}
else
{
ptr=start;
for(i=0;i<p;i++)
{
temp=ptr;
ptr=ptr->next;
if(ptr==NULL)
{
printf("The Position not found.\n");
return;
}
}
temp->next=ptr->next;
printf("The deleted node is %d\n",ptr->data);
free(ptr);
printf("The Node deleted successfully.\n");
}
}
}
void insert_position()
{
struct node *temp,*ptr;
int p,i,count=0;
if(start==NULL)
printf("\nCannot be inserted.");
else
{
printf("\nEnter the position at which data has to be inserted: ");
scanf("%d",&p);
ptr=start;
while(ptr!=NULL)
{
count++;
ptr=ptr->next;
}
if(p>0 && p<=count+1)
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to be inserted: ");
scanf("%d",&temp->data);
if(p==1)
{
temp->next=start;
start=temp;
}
ptr=start;
for(i=1;i<=p-2;i++)
ptr=ptr->next;
temp->next=ptr->next;
ptr->next=temp;
printf("Node insertion done at position\n");
}
else
printf("Position is invalid");
}
}
void search()
{
int x;
struct node *ptr,*end;
printf("\nEnter the data to be searched: ");
scanf("%d",&x);
ptr=start;
while(ptr!=NULL && ptr->data!=x)
ptr=ptr->next;
if(ptr==NULL)
printf("Node with data %d not found",x);
else
printf("Node with %d node found",x);
}
void display()
{
struct node *ptr;
if(start==NULL)
printf("\nLIST IS EMPTY");
else
{
printf("\nLINKED LIST IS: ");
ptr=start;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
}
}
void insert()
{
int ch;
do
{
printf("\n1. INSERT BEFORE\n");
printf("2. INSERT AFTER\n");
printf("3. INSERT AT POSITION\n");
printf("4. EXIT\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insertbefore();
break;
case 2:
insertafter();
break;
case 3:
insert_position();
break;
case 4 :
printf("EXITING.");
break;
}
}
while(ch!=4);
}
void deletes()
{
int ch;
do
{
printf("1. DELETE BY VALUE\n");
printf("2. DELETE BY POSITION\n");
printf("3. EXIT\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
delete_value();
break;
case 2:
delete_position();
break;
case 3:
printf("EXITING.");
break;
}
}
while(ch!=3);
}
void main()
{
int choice;
do
{
printf("\n***** MENU ******\n");
printf("\n1 CREATE LINKED LIST");
printf("\n2 INSERT AN ELEMENT");
printf("\n3 DELETE AN ELEMENT");
printf("\n4 SEARCH");
printf("\n5 DISPLAY");
printf("\n6 EXIT");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2 :
insert();
break;
case 3 :
deletes();
break;
case 4:
search();
break;
case 5:
display();
break;
case 6:
exit(1);
default:
printf("\nInvalid input\n");
break;
}
}while(choice!=6);
}