-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink_list.c
327 lines (265 loc) · 4.69 KB
/
link_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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* head;
void insert(int x);
void delete(int x);
void show();
void search(int x);
void sorting();
int main()
{
int choise=1,x;
while(choise!=6)
{
printf("\n\n ---------------------------------------------------------\n");
printf("\n Link List Operations");
printf("\n 1) Insert");
printf("\n 2) Delete");
printf("\n 3) Show");
printf("\n 4) Search");
printf("\n 5) Sorting");
printf("\n 6) Exit");
printf("\n Enter your choise : ");
scanf("%d",&choise);
switch(choise)
{
case 1: printf("\n Enter data : ");
scanf("%d",&x);
insert(x);
break;
case 2: printf("\n Enter data which you want to delete : ");
scanf("%d",&x);
delete(x);
break;
case 3: show();
break;
case 4: printf("\n Enter the data to search : ");
scanf("%d",&x);
search(x);
break;
case 5: sorting();
printf("\n Sorted link list is : ");
show();
break;
case 6: printf("\n\n Thank you ! \n");
break;
default: printf("\n Please enter vallide choise ..............");
}
}
}
void sorting()
{
struct node*p;
struct node*q;
for(p=head;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(q->data<p->data)
{
int temp;
temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
}
void insert(int x)
{
struct node* p=(struct node*)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
if(head==NULL)//if link list is empty
{
head=p;
}
else//if link list is not empty
{
struct node* q;
q=head;
while(q->next!=NULL)
{
q=q->next;
}
q->next=p;
}
}
void delete(int x)
{
int flag=0;
if(head==NULL)//if link list is empty
{
printf("\n Sorry link list is empty !");
return;
}
else if(head->data==x)//if head node to be deleted
{
struct node* p;
p=head;
head=p->next;
p->next=NULL;
free(p);
flag=1;
}
else
{
struct node* q;
struct node* p;
q=head;
while(q->next!=NULL && q->next->data!=x)
{
q=q->next;
}
if(q->next!=NULL)
{
p=q->next;//p is node which is to be deleted
q->next=p->next;
p->next=NULL;
free(p);
flag=1;
}
}
if(flag==1)
{
printf(" %d is deleted from link list .",x);
}
else
{
printf(" Sorry %d is not present in link list !!!",x);
}
}
void show()
{
struct node* p=head;
if(head==NULL)
{
printf("\n Sorry , link list is empty !!!");
return;
}
while(p!=NULL)
{
printf(" %d ->",p->data);
p=p->next;
}
}
void search(int x)
{
struct node* p=head;
int i=0,flag=0;
while(p!=NULL)
{
i++;
if(p->data==x)
{
printf("\n Element is found at location : %d",i);
flag=1;
break;
}
p=p->next;
}
if(flag==0)
{
printf(" Sorry , %d is not present in link list !",x);
}
}
/* OUTPUT of sorting operation
(base) mansi@mansi-Vostro-15-3568:~$ gcc link_list.c
(base) mansi@mansi-Vostro-15-3568:~$ ./a.out
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 1
Enter data : 11
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 1
Enter data : 99
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 1
Enter data : 44
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 1
Enter data : 88
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 1
Enter data : 22
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 3
11 -> 99 -> 44 -> 88 -> 22 ->
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 5
Sorted link list is : 11 -> 22 -> 44 -> 88 -> 99 ->
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 3
11 -> 22 -> 44 -> 88 -> 99 ->
---------------------------------------------------------
Link List Operations
1) Insert
2) Delete
3) Show
4) Search
5) Sorting
6) Exit
Enter your choise : 6
Thank you !
(base) mansi@mansi-Vostro-15-3568:~$
*/