-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUNCTIONS.C
1293 lines (1207 loc) · 32.1 KB
/
FUNCTIONS.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Stacks
// 0a. c function to push an integer element onto the stack using global variables
// 0b. c function to delete an element from the top of the stack using global variables
// 0c. c function to display the elements of the stack using global variables
// 0d. c function to push an item onto stack using reference variables
// 0e. c function to pop an item from stack using reference variables
// 0f. c function to display the contents of stack using reference variables
// 0g. c function which returns stack precedence values for Conversion of infix to postfix expression (single digit)
// 0h. c function to convert infix to postfix
// 0i. c function which returns stack precedence values for conversion of infix to prefix expression (single digit)
// Queues
// 0j. c function to insert an integer item at the rear end of queue using global variables
// 0k. c function to insert an integer item at the rear end of queue using reference variables(parameters)
// 0l. c function to delete an integer item at the front end of queue using global variables
// 0m. c function to delete an integer item at the front end of queue using reference variables
// 0n. c function to display contents of queue using global variables
// 0o. c function to display contents of queue using parameters
// Double Ended Queue (Dequeue)
// 0p. c function to insert an item at the front end of Double Ended Queue (Dequeue) using global variables
// 0q. c function to delete from rear end of queue using global variables
// Circular Queue
// 0r. c function to insert at the rear end of a circular queue by passing parameters
// 0s. c function to insert at the rear end of a circular queue by using global variables
// 0t. c function to delete at the front end of a circular queue by passing parameters
// 0u. c function to delete at the front end of a circular queue by using global variables
// 0v. c function to display the contents of circular queue by passing parameters
// 0w. c function to display the contents of circular queue by using global variables
// Priority Queue
// 0x. c function to insert an item at the correct place in priority
// Linked List
// a. Node: defined as structure
// b. c function to get node from Available list
// c. c function to free node
// Singly Linked List (SLL)
// d. c function to insert a node at the front end of a SLL
// e. c function to display the contents of a SLL
// f. c function to delete a node/item from the front end of a SLL
// g. c function to insert an item at the rear end of SLL
// h. c function to delete a node/item from the rear end of SLL
// Orderly Linked List
// 1. c function to create an orderly linked list
// Singly Linked List (SLL)
// 2. c function to search for a key in the list
// 3. c function to delete a node whose information field is specified
// 4. c function to delete the node from the specified position
// 5. c function to concatenate two lists
// 6. c function to reverse a list without creating a new node
// 7. c function to insert an item at the specified position
// 1 - 7 function is for Singly Linked List
// Circularly Singly Linked List CSLL
// 8. c function to insert a node at the front end of CSLL
// 9. c function to insert a node at the rear end of CSLL
// 10. c function to delete a node at the front end of CSLL
// 11. c function to delete a node from the rear end of CSLL
// 12. c function to display the contents of the CSLL
// 13. c function to insert a node at the front end using header node of a CSLL
// 14. c function to insert a node at the rear end using header node of a CSLL
// 15. c function to delete the node whose item is specified of CSLL using header node
// 16. c function to delete a node at the front end of CSLL using header node
// 17. c function to delete a node at the rear end of CSLL using header node
// 18. c function to insert a node at the specified position of CSLL using header node
// 19. c function to delete a node from the specified position of CSLL using header node
// 20. c function to display contents of CSLL with header node
// Doubly Linked List
// 21. Structure to define Doubly Linked List (DLL)
// 22. c function to insert a node at the front end of DLL using header node
// 23. c function to insert a node at rear end of DLL using header node
// 24. c function to delete a node from the front end of DLL using header node
// 25. c function to delete a node from the rear end of DLL using header node
// 26. c function to delete a node whose information field (item) is specified of DLL using Header node
// 27. c function to delete all nodes whose information field is specified using DLL with header node
// Applications of Linked List : CSLL with Header node
// 1] Arithmetic operations on long positive numbers
// a)
// 28. c function to read a long positive number
// 0a. c function to push an integer element onto the stack using global variables
void PUSH()
{
if(top==Stack_size-1) // overflow
{
printf(" Stack is full \n");
return;
}
top=top+1; // or s[++top]=item;
s[top]=item;
}
// 0b. c function to delete an element from the top of the stack using global variables
int POP()
{
int item_deleted;
if(top==-1) // underflow
{
return 0;
}
item_deleted = s[top--];
return item_deleted;
}
// 0c. c function to display the elements of the stack using global variables
void display()
{
if(top==-1)
{
printf(" Stack is empty \n"); // underflow
return;
}
printf(" Elements of Stack are \n");
for(int j=0;j<=top;j++)
{
printf("%d\n",s[j]); // s is the stack (array)
}
}
// 0d. c function to push an item onto stack using reference variables
void PUSH(int item,int *top,int s[])
{
if(*top==Stack_size-1) // overflow
{
printf(" Stack is full \n");
return;
}
*top=*top+1; // or s[++top]=item;
s[*top]=item;
}
// 0e. c function to pop an item from stack using reference variables
int POP(int *top,int s[])
{
int item_deleted;
if(*top==-1) // underflow
{
return 0;
}
item_deleted = s[(*top)--]; // obtain the top most element and change the position of top item
return item_deleted;
}
// 0f. c function to display the contents of stack using reference variables
void display(int top,int s[])
{
if(top==-1)
{
printf(" Stack is empty \n"); // underflow
return;
printf("%d\n",s[j]); // s is the stack (array)
}
}
// 0g. c function which returns stack precedence values for Conversion of infix to postfix expression (single digit)
// Function F contains the precedence values of the symbols on top of the stack
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '$':
case '^':return 5;
case '(':return 0;
case '#':return -1;
default: return 8; // break not required as return statement is there
}
}
// Function G contains the precedence values of symbols in the input string
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '$':
case '^':return 6;
case '(':return 9;
case ')':return 0;
default: return 7; // break not required as return statement is there
}
}
// 0h. c function to convert infix to postfix
void infix_Postfix(char infix[],char postfix[])
{
int top;
int i,j;
char s[30];
char symbol;
top=-1;
s[++top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol)) // as long as precedence value of the symbol on top of the stack is greater than the precedence value of the current input symbol,pop an item from the stack and place it in the postfix expression
{
Postfix[j++]=s[top--]; // pop an item from stack and place it in postfix
}
if(F(s[top])!=G(symbol)) // once the condition in while loop fails and if the precedence of the symbol on top of the stack is not equal to the precedence values of the current input symbol push the current symbol onto the stack otherwise delete an item from the stack but do not place it in the postfix expression
{
s[++top]=symbol;
}
else
{
top--;
}
}
while(s[top]!='#')
{
Postfix[j++]=s[top--];
}
Postfix[j]='\0';
}
// 0i. c function which returns stack precedence values for conversion of infix to prefix
// Function F contains the precedence values of the symbols on top of the stack
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '$':
case '^':return 6;
case ')':return 0;
case '#':return -1;
default: return 8;// operands is default // break not required as return statement is there
}
}
// Function G contains the precedence values of symbols in the input string
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '$':
case '^':return 3;
case '(':return 0;
case ')':return 9;
default: return 7; // break not required as return statement is there
}
}
// conversion of infix to prefix
/* write function for F and G .Reverse the infix expression and follow the same procedure to convert infix to postfix .Prefix expression is obtained by reversing the result */
// 0j. c function to insert an integer item at the rear end of queue using global variables
void insert_rear()
{
int item;
if(r==Queue_size-1)
{
printf(" Queue overflow \n");
return;
}
r=r+1;
q[r]=item; // or q[++r]=item;
}
// 0k. c function to insert an integer item at the rear end of queue using reference variables(parameters)
void insert_rear(int item,int *r,q[])
{
if(*r==Queue_size-1)
{
printf(" Queue overflow \n");
return;
}
*r=*r+1;
q[*r]=item; // or q[++(*r)]=item;
}
// 0l. c function to delete an integer item at the front end of queue using global variables
void delete_front()
{
if(f>r)
{
printf(" Queue is empty \n");
return;
}
printf(" Item deleted from queue %d \n",q[f++]);
if(f>r) // after reading an item it may so happen that queue may be empty so immediately after deleting an element if q is empty initialize f=0 and r=-1
{
f=0;
r=-1;
}
}
// 0m. c function to delete an integer item at the front end of queue using reference variables
void delete_front(int q[],int *f,int *r)
{
if(*f>*r)
{
printf(" Queue is empty \n");
return;
}
printf(" Item deleted from queue %d \n",q[(*f)++]);
if(*f>*r) // after reading an item it may so happen that queue may be empty so immediately after deleting an element if q is empty initialize f=0 and r=-1
{
*f=0;
*r=-1;
}
}
// 0n. c function to display contents of queue using global variables
void display()
{
if(f>r)
{
printf(" Queue is empty \n");
return;
}
printf(" The contents of queue are \n");
for(int i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
// 0o. c function to display contents of queue using parameters
void display(int q[],int f,int r)
{
if(f>r)
{
printf(" Queue is empty \n");
return;
}
printf(" The contents of queue are \n");
for(int i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
// 0p. c function to insert an item at the front end of Double Ended Queue (Dequeue) using global variables
void insert_front()
{
if(f==0 && r==-1) //when queue is empty,item can be inserted at front end by incrementing r by 1 and then insert an item
{
q[++r]=item;
return;
}
if(f!=0)
{
q[--f]=item; // some items are deleted at front but queue is not empty
return;
}
printf(" Insertion not possible \n"); // some items are inserted but not deleted
}
// 0q. c function to delete from rear end of queue using global variables
void delete_rear()
{
if(f>r)
{
printf(" Queue is empty\n");
return;
}
printf(" The element deleted is %d\n",q[r--]);
if(f>r)
{
f=0;
r=-1;
}
}
// 0r. c function to insert at the rear end of a circular queue by passing parameters
void insert_rear(int item,int q[],int *r,int *count)
{
if(*count==Queue_size)
{
printf(" Queue overflow \n");
return;
}
*r=(*r+1)%Queue_size;
q[*r]=item;
(*count)++;
}
// 0s. c function to insert at the rear end of a circular queue by using global variables
void insert_rear(int item,int q[],int r,int count)
{
if(count==Queue_size)
{
printf(" Queue overflow \n");
return;
}
r=(r+1)%Queue_size;
q[r]=item;
count++;
}
// 0t. c function to delete at the front end of a circular queue by passing parameters
void delete_front(int q[],int *f,int *count)
{
if((*count)==0)
{
printf(" Queue underflow \n");
return;
}
printf(" The item deleted is %d\n",q[*f]);
*f=(*f+1)%Queue_size;
(*count)--;
}
// 0u. c function to delete at the front end of a circular queue by using global variables
void delete_front(int q[],int f,int count)
{
if(count==0)
{
printf(" Queue underflow \n");
return;
}
printf(" The item deleted is %d\n",q[f]);
f=(f+1)%Queue_size;
count--;
}
// 0v. c function to display the contents of circular queue by passing parameters
void display(int q[],int f,int count)
{
int i;
if(count==0)
{
printf(" Queue is empty\n");
return;
}
printf(" Contents of queue are \n");
for(i=1;i<=count;i++)
{
printf("%d\n",q[f]);
f=(f+1)%Queue_size;
}
}
// 0w. c function to display the contents of circular queue by using global variables
void display()
{
int i;
if(count==0)
{
printf(" Queue is empty\n");
return;
}
printf(" Contents of queue are \n");
for(i=1;i<=count;i++)
{
printf("%d\n",q[f]);
f=(f+1)%Queue_size;
}
}
// 0x. c function to insert an item at the correct place in priority
void insert_item(int item,int q[],int *r)
{
int j;
if(*r==Queue_size-1)
{
printf(" Queue is full\n");
return;
}
// find appropriate position to make room for inserting an item based on priority
j=*r;
while(j>=0 && item<=q[j])
{
q[j+1]=q[j];
j--;
}
q[j+1]=item;
*r=*r+1;
}
/* Implementation : Assume the item to be inserted itself denotes the priority. The item with least value has highest priority and items with highest value has least priority
therefore insert the elements into the queue in such a way that they are always ordered in increasing order .that is ;highest priority elements are at the front end
of the queue and lowest priority elements are at the rear end of the queue.therefore by deleting an item always delete from the front end so that highest priority element is deleted
first */
// a. Node: defined as structure
struct node //header
{
int info; // can be any data type
struct node *link; // self referential structure
};
typedef struct node *NODE;
// A pointer variable first can be declared as
// method 1 NODE first
// method 2 struct node *first
// the info and link field can be accessed using the following notations
// 1
// first->info
// first->link
// 2
// (*first).info
// (*first).link
// b. c function to get node from Available list
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(" Out of memory \n");
exit(0); // or return;
}
return x;
}
// c. c function to free node
void freenode(NODE x)
{
free(x);
}
// d. c function to insert a node at the front end of a SLL
NODE insert_front(int item,NODE first)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
}
// e. c function to display the contents of a SLL
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf(" List is empty\n");
return;
}
printf(" Contents of SLL are \n");
temp=first;
while(temp!=NULL)
{
printf("%d",temp->info);
temp=temp->link;
}
printf("\n");
}
// f. c function to delete a node/item from the front end of a SLL
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf(" List is empty and hence cannot be deleted \n");
return;
}
temp=first;
temp=temp->link;
printf(" Item deleted is %d\n",first->info);
freenode(first);
return temp; // temp is now the first node of the list
}
// in void main
// NODE first=NULL; // to start , List is empty
// g. c function to insert an item at the rear end of SLL
NODE insert_rear(int item,NODE first)
{
NODE temp,cur;
temp=getnode(); // create a node to be inserted
temp->info=item; // and insert the item
temp->link=NULL;
if(first==NULL) // if the node temp is inserted into the list for the first time i.e when list is empty
{
return temp; // then return temp itself as the first node
}
cur=first; // with the existing list, obtain the address of the last node,use pointer cur (current) which initially points to first node
while(cur->link!=NULL)
{
cur=cur->link; // obtain the address of last node :update cur till the last node is reached
// if cur->link contains '\0' the cur points to last node
}
cur->link=temp; // insert temp at the end of the list
return first; // return the address of first node
}
// h. c function to delete a node/item from the rear end of SLL
NODE delete_rear(NODE first)
{
NODE cur,prev;
if(first==NULL) // list is empty hence it is not possible to delete an item
{
printf(" List is empty\n"); // so we print appropriate message
return first;
}
if(first->link==NULL) // list has only 1 node
{
printf(" Item to be deleted is %d\n",first->info);
freenode(first);
return NULL; // now list becomes empty
}
// list has more than 1 node
// to delete the last node, we need to find the address of the last node and the address of last but one node use 2 pointers cur and prev
// initially cur points to first node and prev points to null
// repeatedly update cur and prev as long as the link field of cur is not null
prev=NULL;
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
printf(" Item deleted is %d\n",cur->info);
freenode(cur); // delete the last node pointed by cur using function free node
prev->link=NULL; // the node pointed to by prev is the last node ans therefore null is copied into the link field of prev
return first; // return the address of first node
}
// 1. c function to create an orderly linked list
NODE insert_order(int item,NODE first)
{
NODE temp,prev,cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
{
return temp;
}
if(item<first->info)
{
temp->link=first;
return temp;
}
prev=NULL;
cur=first;
while(cur!=NULL && item>cur->info)
{
prev=cur;
cur=cur->link;
}
// insert node between prev and cur
prev->link=temp;
temp->link=cur;
return first;
}
// 2. c function to search for a key in the list
void search(int key,NODE first)
{
NODE cur;
if(first==NULL)
{
printf(" The list is empty\n");
return;
}
cur=first;
while(cur!=NULL)
{
if(key==cur->info)
{
break;
}
cur=cur->link;
}
if(cur==NULL)
{
printf(" Search is unsuccessful \n");
return;
}
printf(" Search is successful\n");
}
// 3. c function to delete a node whose information field is specified
NODE delete_info(int key,NODE first)
{
NODE prev,cur;
if(first==NULL)
{
printf(" List is empty \n");
return NULL;
}
if(key==first->info)
{
cur=first;
first=first->link;
freenode(cur);
return first;
}
prev=NULL;
cur=first;
while(cur!=NULL)
{
if(key==cur->info)
break;
prev=cur;
cur=cur->link;
}
if(cur==NULL)
{
printf(" Search is unsuccessful\n");
return first;
}
prev->link=cur->link;
freenode(cur);
return first;
}
// 4. c function to delete the node from the specified position
NODE delete_pos(int pos,NODE first)
{
int count;
NODE prev,cur;
if(first==NULL || pos<=0)
{
printf(" Invalid position\n");
return NULL;
}
if(pos==1)
{
cur=first;
first=first->link;
freenode(cur);
return first;
}
prev=NULL;
cur=first;
count=1;
while(cur!=NULL)
{
if(count==pos)
break;
prev=cur;
cur=cur->link;
count++;
}
if(cur==NULL)
{
printf(" Search is unsuccessful\n");
return first;
}
prev->link=cur->link;
freenode(cur);
return first;
}
// 5. c function to concatenate two lists
NODE concate(NODE first,NODE second)
{
NODE cur;
if(first==NULL)
return second;
if(second==NULL)
return first;
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=second;
return first;
}
// 6. c function to reverse a list without creating a new node
NODE reverse(NODE first)
{
NODE cur,temp;
cur=NULL; //initial reverse list
while(first!=NULL)
{
temp=first;
first=first->link;
temp->link=cur;
cur=temp;
}
return cur;
}
// 7. c function to insert an item at the specified position
NODE insert_pos(int item,int pos,NODE first)
{
NODE temp,prev,cur;
int count;
temp=getnode(); // create a node with item given
temp->info=item;
temp->link=NULL:
if(first==NULL)
return temp; //inserting the node for the first time
if(pos==1)
{
temp->link=first;
return temp; //for the first position
}
// to find the appropriate position
count=1;
prev=NULL;
cur=first;
while(cur!=NULL && count!=pos)
{
prev=cur;
cur=cur->link;
count++;
}
if(count==pos)
{
prev->link=temp;
temp->link=cur;
return first;
}
printf(" Invalid position\n");
return first;
}
// 8. c function to insert a node at the front end of CSLL
NODE insert_front(int item,NODE last)
{
NODE temp;
temp=getnode();
temp->info=item; //place item in info field of temp
temp->link=NULL; // create a node
if(last==NULL) // list is empty
{
last=temp; // temp itself is the last node
}
else
{
temp->link=last->link;
}
last->link=temp;
return last;
}
// 9. c function to insert a node at the rear end of CSLL
NODE insert_rear(int temp,NODE last)
{
NODE temp;
temp=getnode(); // create a node
temp->info=item;
if(last==NULL)
{
last=temp;
}
else
{
temp->link=last->link;
}
last->link=temp;
return temp;
}
// 10. c function to delete a node at the front end of CSLL
NODE delete_front(NODE last)
{
NODE temp,first;
if(last==NULL) // list is empty
{
printf(" List is empty \n");
return NULL;
}
if(last->link==last) //list has 1 node
{
printf(" Item deleted is %d \n",last->info);
freenode(last);
return NULL;
}
first=last->link; // list has more than 1 node
last->link=first->link;
printf(" Item deleted is %d \n",first->info);
freenode(first);
return last;
}
// 11. c function to delete a node from the rear end of CSLL
NODE delete_rear(NODE last)
{
NODE prev;
if(last==NULL) // list is empty
{
printf(" List is empty \n");
return NULL;
}
if(last->link==last) // list has only 1 node
{
printf(" Item deleted is %d \n",last->info);
freenode(last);
return NULL; // it becomes empty list
}
prev=last->link; // first node in the list ,traversing from first node
while(prev->link!=last)
{
prev=prev->link;
}
prev->link=last->link;
printf(" Item deleted is %d \n",last->info);
freenode(last);
return prev; // prev is the last node in the list now
}
// 12. c function to display the contents of the CSLL
void display(NODE last)
{
NODE temp;
if(last==NULL) // list is empty
{
printf(" List is empty\n");
return;
}
printf(" Contents of the list are \n");
temp=last->link; // address of the first node
while(temp!=last)
{
printf("%d",temp->info);
temp=temp->link;
} // until we reach the last node
printf("%d",temp->info); // display the content/info of last node
printf("/n");
}
/* A HEADER NODE in a linked list is a specified node,whose link field always contains
the address of the first node of the list */
// in void main
// NODE head;
// head=getnode(); get node function
// head->info=0;
// head->link=head;
// 13. c function to insert a node at the front end using header node of a CSLL
NODE insert_front(int item,NODE head)
{
NODE temp;
temp=getnode(); // create a node
temp->info=item;
temp->link=head->link;
head->link=temp;
return head;
}
// 14. c function to insert a node at the rear end using header node of a CSLL
NODE insert_rear(int item,NODE head)
{
NODE temp,cur;
temp=getnode(); // create a node
temp->info=item;
cur=head->link; // address of first node
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;
}
// 15. c function to delete the node whose item is specified of CSLL using header node
NODE delete_item(int item,NODE head)
{
NODE prev,cur;
if(head->link==head) // list is empty
{
printf(" List is empty\n");
return NULL;
}
prev=head;
cur=head->link;
while(cur!=head)
{
if(item==cur->info)
{
break;
}
prev=cur;
cur=cur->link;
}
if(cur==head)
{
printf(" Item not found \n");
return head;
}
prev->link=cur->link;