-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathLinked_list .C
444 lines (413 loc) · 8.76 KB
/
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include<stdio.h>
#include<stdlib.h>
/*****************************************/
struct node{
int info;
struct node *Next;
};
/*****************************************/
struct node* GetNode()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
return p;
}
/*****************************************/
/******Insert the node a beginning*******/
void InsBeg( struct node **START,int x)
{
struct node *p;
p=GetNode();
p->info=x;
p->Next=(*START);
(*START)=p;
}
/*****************************************/
/**********Inset the node at end**********/
void InsEnd(struct node **START,int x)
{
struct node *p;
struct node *q;
if((*START)==NULL)
InsBeg(&(*START),x);
else
{
p=(*START);
while(p->Next!=NULL)
{
p=p->Next;
q=GetNode();
}
q->info=x;
p->Next=q;
q->Next=NULL;
}
}
/*****************************************/
/*****Delete the node at beginning********/
int DelBeg(struct node **START)
{
int x;
struct node *p;
if(START==NULL)
{
printf("void delection");
exit(1);
}
else
{
p=(*START);
(*START)=p->Next;
x=p->info;
free(p);
return x;
}
}
/*****************************************/
/*****Delete the node at end**************/
int DelEnd(struct node **START)
{
int x;
struct node *p;
struct node *q;
q=NULL;
p=(*START);
while(p->Next!=NULL)
{
q=p;
p=p->Next;
}
q->Next=NULL;
x=p->info;
free(p);
return x;
}
/*****************************************/
/*****Search the given node**************/
int Searching(struct node **START)
{
int x;
printf("enter the search element");
scanf("%d",&x);
struct node *p;
p=(*START);
int i=1;
while(p!=NULL)
{
if(p->info==x)
break;
else
i++;
p=p->Next;
}
return i;
}
/*****************************************/
/*****Search the given node**************/
struct node* Search(struct node **START,int x)
{
/* printf("enter the search element");
scanf("%d",&x);*/
struct node *p;
struct node *q;
p=(*START);
q=NULL;
while(p!=NULL)
{
if(p->info==x)
{
return p;
}
p=p->Next;
}
return NULL;
}
/*****************************************/
/**Insert the node after the given node***/
void InsAft( struct node **p,int x)
{
struct node *q;
/* p=Search(&START);
int x;*/
/*printf("enter the insert element");
scanf("%d",&x);*/
q=GetNode();
q->info=x;
q->Next=(*p)->Next;
(*p)->Next=q;
}
/*****************************************/
/**Delete the node after the given node***/
int DelAft(struct node **p)
{
int x;
struct node *q;
if((*p)==NULL||(*p)->Next==NULL)
{
printf("VOID DELECTION");
}
else
{
q=(*p)->Next;
(*p)->Next=q->Next;
x=q->info;
free(q);
}
return x;
}
/*****************************************/
/**Insert the node in Ascending order ****/
void OrdIns(struct node **START,int x)
{
struct node *p,*q;
q=NULL;
p=(*START);
while(p!=NULL&&x>p->info)
{
q=p;
p=p->Next;
}
if(q!=NULL)
InsAft(&q,x);
else
InsBeg((START),x);
}
/*****************************************/
/******Delete the specific node**********/
void DelSpec(struct node **START,int x)
{
struct node *p,*q;
p=(*START);
q=NULL;
while(p!=NULL)
{
if(p->info==x)
{
p=p->Next;
if(q==NULL)
{
DelBeg(&(*START));
}
else
{
DelAft(&q);
}
}
else
q=p;
p=p->Next;
}
}
/*****************************************/
/***Reverse the given linked list********/
void Reverse(struct node **START)
{
struct node *p,*q,*r;
p=(*START);
r=(*START)->Next;
while(r!=NULL)
{
q=r->Next;
r->Next=p;
p=r;
r=q;
}
(*START)->Next=NULL;
(*START)=p;
}
/*****************************************/
/*****Concatenate of two linked list***********/
Concinate(struct node **START,struct node **START1)
{
struct node *p;
if((*START)==NULL)
{
return (*START1);
}
else
{
p=(*START);
while(p->Next!=NULL)
{
p=p->Next;
}
}
p->Next=(*START1);
return (*START);
}
/*****************************************/
/****Counting of linked list node********/
int Counting(struct node **START)
{
struct node *p;
int count=0;
p=(*START);
while(p!=NULL)
{
count++;
p=p->Next;
}
printf("Count of node =%d",count);
}
/*****************************************/
/**counting of odd and even linked list information*/
void EvenOddCount(struct node **START)
{
struct node *p;
p=(*START);
int Ecount,Ocount;
Ecount=0;
Ocount=0;
while(p!=NULL)
{
{
if(p->info%2==0)
Ecount++;
else
Ocount++;
}
p=p->Next;
}
printf("Even node is %d and Odd node is %d",Ecount,Ocount);
}
/*****************************************/
/*******Delete the alternate node********/
DelectAlter(struct node **START)
{
struct node *p,*q;
p=(*START);
q=NULL;
int i=1;
while(p!=NULL)
{
if(i%2==0)
{
p=p->Next;
DelAft(&q);
}
else
q=p;
p=p->Next;
}
i++;
}
/*****************************************/
/********Sorting of linked list***********/
SortLinkedList(struct node **START)
{
struct node *p, *q;
int temp;
p = (*START);
while(p -> Next!= NULL)
{
q = p->Next;
while(q!= NULL)
{
if(p->info > q->info)
{
temp=p->info;
p->info = q->info;
q->info= temp;
}
q=q->Next;
}
p=p->Next;
}
return (*START);
}
/*****************************************/
/*****Traverse the linked list***********/
void Traverse(struct node *START)
{
struct node *p;
p=START;
while(p!=NULL)
{
printf(" %d ",p->info);
p=p->Next;
}
}
/*****************************************/
/***********Main Function*****************/
int main()
{ int x;
struct node *START,*START1,*START2;
START=NULL;
START1=NULL;
START2=NULL;
printf("INSERT AT BEG\n");
InsBeg(&START,500);
InsBeg(&START,400);
InsBeg(&START,300);
InsBeg(&START,200);
InsBeg(&START,100);
Traverse(START);
printf("\nINSERT AT END\n");
InsEnd(&START,600);
InsEnd(&START,700);
InsEnd(&START,800);
printf("\n\n");
Traverse(START);
printf("\nDELECT AT BEG");
DelBeg(&START);
printf("\n\n");
Traverse(START);
printf("\nDELECT AT END");
DelEnd(&START);
printf("\n\n");
Traverse(START);
printf("\nINSERT AFTER 200\n");
struct node* i;
i= Search(&START,200);
InsAft(&i,1000);
printf("\n\n");
Traverse(START);
printf("\nDELECT AFTER 500");
printf("\n\n");
i= Search(&START,500);
DelAft(&i);
Traverse(START);
printf("\n\n");
printf("LINKED LIST 1");
Traverse(START);
printf("\n\n");
printf("LINKED LIST 2");
InsBeg(&START1,1500);
InsBeg(&START1,1400);
InsBeg(&START1,1300);
InsBeg(&START1,1200);
InsBeg(&START1,100);
InsBeg(&START1,800);
Traverse(START1);
printf("\n\n");
printf("CONICATE THE LINKED LIST");
START2=Concinate(&START,&START1);
Traverse(START2);
printf("\n\n");
printf("SORTING OF LINKED LIST");
printf("\n\n");
SortLinkedList(&START2);
Traverse(START2);
printf("\n\n");
printf("Adding the linked list in ascending order \n");
OrdIns(&START2,100);
OrdIns(&START2,250);
OrdIns(&START2,350);
OrdIns(&START2,450);
OrdIns(&START2,500);
Traverse(START2);
printf("\n\n");
printf("Delete the specific item ");
printf("\n\n");
DelSpec(&START2,700);
Traverse(START);
printf("\n\n");
Counting(&START);
printf("\n\n");
EvenOddCount(&START);
printf("\n\n");
printf("REVERSE THE LINKED LIST \n");
Reverse(&START);
Traverse(START);
/*****************************************/
}