-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataStructures.c
More file actions
executable file
·1422 lines (1245 loc) · 41.4 KB
/
dataStructures.c
File metadata and controls
executable file
·1422 lines (1245 loc) · 41.4 KB
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
/************************************************************************************************
* dataStructures.c
*
* Last Modified Date Author Description
*
* 14-Aug-2015 Deepak Birla Initial version
*
***********************************************************************************************/
/***********************************************************************************************
USING THIS PROGRAM WE CAN PERFORM BASIC LINK LIST OPERATIONS,
STACK OPERATION, QUEUE OPERATIONS AND BINARY TREE OPERATIONS
* ENTER N LISTS
* CREATE NODE
* CREATE LIST
* INSERT NODE
* DELETE NODE
* REVERSE LIST
* SEARCH NODE IN LIST
* SORT LIST
* DELETE K NODES FROM LIST
* DISPLAY LIST
* DETECT LOOP IN LIST (FLOYD ALGORITHM)
* MAKE LIST CIRCULAR
* DISPLAY CIRCULAR LIST
* INSERT NODE IN CIRCULAR LIST
* DELETE NODE IN CIRCULAR LIST
* CONCATENATE TWO LISTS
* COMPARE TWO LISTS
* DISPLAY LIST AS STACK
* PUSH ELEMENT IN STACK
* POP ELEMENT FROM STACK
* DISPLAY LINK LIST AS QUEUE
* INSERT IN QUEUE
* DELETE FROM QUEUE
* STACK SIZE
* QUEUE SIZE
* CREATE TREE
* PREORDER TRAVERSAL OF TREE
* INORDER TRAVERSAL OF TREE
* POSTORDER TRAVERSAL OF TREE
* SEARCH NODE IN TREE
* DEPTH OF TREE
* DELETE COMPLETE TREE
* DELETE A NODE FROM TREE
************************************************************************************************/
/************************** HEADER FILES INCLUDED IN THE PROGRAM *****************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/************************************ GLOBAL VARIABLES ***************************************/
int numberOfNodes=0,treeNodes=0;
/************************************************************************************************
STRUCTURE DEFINITION.THIS STRUCTURE DEFINES A NODE WHICH HAS TWO VALUES
* DATA
* ADDRESS OF ANOTHER NODE
************************************************************************************************/
struct _node
{
int data;
struct _node *next;
}*start=NULL,*current,**start_node=&start,*end=NULL;
/************************************************************************************************
STRUCTURE DEFINITION.THIS STRUCTURE DEFINES A NODE WHICH HAS THREE VALUES
* VALUE
* ADDRESS OF RIGHT NODE IN TREE
* ADDRESS OF LEFT NODE IN TREE
************************************************************************************************/
struct _tree_node
{
int value;
struct _tree_node *left;
struct _tree_node *right;
}*root=NULL,**root_node=&root;
/******************************************************************** LINKED LISTS, STACKS AND QUEUES FUNCTIONS **************************************************************************/
/************************************************************************************************
THIS METHOD DYNAMICALLY CREATES A NODE AND RETURNS ADDRESS OF TEHT NODE
************************************************************************************************/
struct _node* create_node(int value)
{
struct _node *new_node;
new_node=(struct _node *)malloc(sizeof(struct _node));
new_node->data=value;
new_node->next=NULL;
return new_node;
}
/************************************************************************************************
THIS METHOD REVERSE THE LIST
************************************************************************************************/
void reverse_list()
{
struct _node *temp,*temp1,*var;
temp=start;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
var->next=temp1;
}
start=var;
}
/************************************************************************************************
THIS METHOD DELETES K NODE FROM THE LIST
************************************************************************************************/
void delete_knodes_from_list( int k)
{
int i;
struct _node *new_node,*n;
while(k!=0)
{
new_node=start;
while(new_node!=NULL)
{
if(new_node->next->next==NULL)
{
n=new_node->next;
new_node->next=NULL;
free(n);
}
new_node=new_node->next;
}
k--;
}
}
/************************************************************************************************
THIS METHOD SORTS THE LINK LIST
************************************************************************************************/
void sort_list()
{
int d;
struct _node *temp,*temp1;
temp=start;
temp1=start;
while(temp!=NULL)
{
temp1=start;
while(temp1!=NULL)
{
if(temp1->next!=NULL)
{ if(temp1->data > temp1->next->data)
{
d=temp1->data;
temp1->data=temp1->next->data;
temp1->next->data=d;
}
}
temp1=temp1->next;
}
temp=temp->next;
}
}
/************************************************************************************************
THIS METHOD SEARCH A VALUE IN THE LIST AND RETURNS NODE POSITION
************************************************************************************************/
int search_in_list(int s)
{
int count=0,flag=0;
struct _node *temp;
temp=start;
count++;
while(temp!=NULL)
{
if(temp->data==s)
{
flag=1;
break;
}
else
flag=0;
temp=temp->next;
count++;
}
if(flag==1)
return count;
else
return-1;
}
/************************************************************************************************
THIS METHOD MAKES THE LINK LIST CIRCULAR
************************************************************************************************/
void make_circular_list()
{
struct _node *new_node;
new_node=start;
if(start->next !=NULL)
{
while(new_node->next != NULL)
{
new_node=new_node->next;
}
new_node->next=start;
}
else
printf("\nNot Enough nodes to create Circular Link List");
}
/************************************************************************************************
THIS METHOD COUNTS THE NUMBER OF NODES PRESENT IN LIST AND RETURN COUNT
************************************************************************************************/
int count_nodes_in_list()
{
int count=0;
struct _node *new_node;
new_node=start;
while(new_node != NULL)
{
new_node=new_node->next;
count++;
}
return count;
}
/************************************************************************************************
THIS METHOD COMPARE TWO SINGLY LINK LISTS
************************************************************************************************/
void compare_list(num1,num2)
{
struct _node *new_node,*new_node1;
int count1=0,count2=0,max,flag=0;
new_node=*(start_node + num1 - 1);
new_node1=*(start_node + num2 - 1);
while(new_node->next !=NULL)
{
count1++;
new_node=new_node->next;
}
while(new_node1->next !=NULL)
{
count2++;
new_node1=new_node1->next;
}
if(count1>count2)
printf("\nList 1 is greater then List 2");
else if(count2>count1)
printf("\nList 2 is greater then List 1");
else
{
new_node=*(start_node + num1 - 1);
new_node1=*(start_node + num2 - 1);
while(new_node !=NULL)
{
if(new_node->data == new_node1->data)
flag=1;
else if(new_node->data > new_node1->data)
{
printf("\nList 1 is greater then List 2");
break;
}
else if(new_node->data < new_node1->data)
{
printf("\nList 1 is greater then List 2");
break;
}
new_node=new_node->next;
new_node1=new_node1->next;
}
if(flag==1)
printf("\nList one and List two are not equal");
}
}
/************************************************************************************************
THIS METHOD CONCATENATES TWO SINGLY LINK LISTS
************************************************************************************************/
void concatenate_list(num1 , num2)
{
struct _node *new_node;
start=*(start_node + num1 -1);
new_node=*(start_node + num1 -1);
while(new_node->next !=NULL)
{
new_node=new_node->next;
}
new_node->next=*(start_node + num2 -1);
*(start_node + num1 -1)=start;
*(start_node + num2 -1)=start;
}
/************************************************************************************************
THIS METHOD ALLOW USER TO ENTER LIST NUMBER ON WHICH USER WANTS TO WORK.
AND RETURNS THE LIST NUMBER
************************************************************************************************/
int choose_list(int number_of_lists)
{
int ch;
char term;
do
{
system("clear");
__fpurge(stdin);
printf("\nEnter List Number (1 to %d):",number_of_lists);
}while(scanf("%d%c", &ch, &term) != 2 || term != '\n' || ch > number_of_lists || ch < 1);
return ch;
}
/************************************************************************************************
THIS METHOD DETECTS IF THERE IS ANY LOOP PRESENT IN THE LIST
AND RETURNS 1 IF LOOP IS PRESENT ELSE IT RETURNS 0
************************************************************************************************/
int detect_loop_in_list()
{
struct _node *new_node,*f,*s;
int flag=0;
new_node=start;
f= new_node;
s= new_node;
while(f && s && f->next)
{
s=s->next;
f=f->next->next;
if(s==f)
{
flag=1;
break;
}
else
flag=0;
}
return flag;
}
/************************************************************************************************
THIS METHOD POPS ELEMENT FROM STACK
************************************************************************************************/
int delete_from_stack()
{
struct _node *new_node;
int temp;
new_node=start;
if(start == NULL)
return -99;
else
{
temp=new_node->data;
start=start->next;
free(new_node);
}
return temp;
}
/************************************************************************************************
THIS METHOD POPS ELEMENT FROM Queue
************************************************************************************************/
int delete_from_queue()
{
struct _node *new_node;
int temp;
new_node=start;
if(start == NULL)
return -99;
else
{
temp=new_node->data;
start=start->next;
free(new_node);
}
return temp;
}
/************************************************************************************************
THIS METHOD PUSH ELEMENT IN STACK
************************************************************************************************/
int insert_in_stack(int value)
{
struct _node *new_node,*temp;
new_node=start;
temp=create_node(value);
start=temp;
temp->next=new_node;
return temp->data;
}
/************************************************************************************************
THIS METHOD PUSH ELEMENT IN QUEUE
************************************************************************************************/
int insert_in_queue(int value)
{
struct _node *new_node,*temp;
if(end == NULL)
return -99;
else
{
temp=create_node(value);
end->next=temp;
end=temp;
return temp->data;
}
}
/************************************************************************************************
THIS METHOD DISPLAYS THE LINK LIST AS STACK
************************************************************************************************/
void display_stack()
{
struct _node *new_node;
new_node=start;
if(start==NULL)
printf("\nThe STACK is empty");
else
{
printf("\nThe STACK :");
while(new_node!=NULL)
{
printf("\n\t %d",new_node->data);
printf("\n\t--------");
new_node=new_node->next;
}
}
}
/************************************************************************************************
THIS METHOD DELETES A NODE FROM THE LINK LIST
************************************************************************************************/
void delete_node_from_list( int p)
{
int i;
struct _node *new_node;
new_node=start;
if(p==1)
{
start=start->next;
free(new_node);
}
else
{
for(i=1;i<p-1;i++)
new_node=new_node->next;
new_node->next=new_node->next->next;
}
}
/************************************************************************************************
THIS METHOD REMOVES DUPLICATE ELEMENTS FROM THE LIST
************************************************************************************************/
void remove_duplicate_elements_from_list()
{
int i;
struct _node *new_node,*temp;
new_node=start;
while(new_node->next !=NULL)
{
if(new_node->data == new_node->next->data)
{
temp=new_node->next;
new_node->next = new_node->next->next;
free(temp);
}
new_node=new_node->next;
}
}
/************************************************************************************************
THIS METHOD DELETES A NODE FROM A CIRCULAR LINK LIST
************************************************************************************************/
void delete_node_from_circular_list( int p)
{
int i;
struct _node *new_node;
new_node=start;
for(i=1;i<p-1;i++)
new_node=new_node->next;
new_node->next=new_node->next->next;
numberOfNodes--;
}
/************************************************************************************************
THIS METHOD INSERTS A NODE AT THE GIVE POSITION IN THE LIST
************************************************************************************************/
void insert_node_in_list( int pos,int value)
{
int i;
struct _node *new_node,*f;
struct _node *current;
current=create_node(value);
new_node=start;
if(pos==1)
{
f=start;
start=current;
current->next=f;
}
else
{
for(i=1;i<pos-1;i++)
new_node=new_node->next;
f=new_node->next;
new_node->next=current;
current->next=f;
}
}
/************************************************************************************************
THIS METHOD INSERTS A NODE IN THE CIRCULAR LINK LIST AT GIVEN POSITION
************************************************************************************************/
void insert_node_in_circular_list( int pos,int value)
{
int i;
struct _node *new_node,*f;
struct _node *current;
current=create_node(value);
new_node=start;
for(i=1;i<pos-1;i++)
new_node=new_node->next;
f=new_node->next;
new_node->next=current;
current->next=f;
numberOfNodes++;
}
/************************************************************************************************
THIS METHOD CREATES A LINK LIST
************************************************************************************************/
void add_node_to_list(struct _node *new_node)
{
__fpurge(stdin);
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
/************************************************************************************************
THIS METHOD DISPLAYS THE LINK LIST
************************************************************************************************/
void display_list()
{
struct _node *new_node;
int flag=0;
flag=detect_loop_in_list();
if(flag==1)
printf("\nLoop Detected :Can't Display Link List. Try option 12 to Display circular Link List");
else
{
printf("\nThe Linked List : ");
new_node=start;
if(flag!=1)
{
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->next;
}
printf("NULL\n");
}
}
}
/************************************************************************************************
THIS METHOD DISPLAYS THE QUEUE
************************************************************************************************/
void display_queue()
{
struct _node *new_node;
int flag=0;
flag=detect_loop_in_list();
if(flag==1)
printf("\nLoop Detected :Can't Display Queue");
else
{
printf("\nThe Queue is : ");
new_node=start;
if(flag!=1)
{
while(new_node!=NULL)
{
printf("%d<---",new_node->data);
new_node=new_node->next;
}
}
}
}
/************************************************************************************************
THIS METHOD WILL CONVERT LIST TO QUEUE
************************************************************************************************/
void convert_list_to_queue()
{
struct _node *new_node;
new_node=start;
while(new_node->next != NULL)
{
new_node=new_node->next;
}
end=new_node;
}
/************************************************************************************************
THIS METHOD DISPLAYS THE CIRCULAR LINK LIST
************************************************************************************************/
void display_circular_list()
{
struct _node *new_node;
int n=numberOfNodes;
printf("\nThe Linked List : ");
new_node=start;
while(n!=0)
{
printf("%d--->",new_node->data);
new_node=new_node->next;
n--;
}
}
/***************************************************************** LINKED LISTS, STACKS AND QUEUES FUNCTIONS OVER ************************************************************************/
/***************************************************************************** BINARY SEARCH TREE ****************************************************************************************/
/************************************************************************************************
METHOD TO CREATE A NODE FOR A TREE AND RETURNS THE NODE ADDRESS
************************************************************************************************/
struct _tree_node* create_tree_node(int data)
{
char term;
struct _tree_node *new_node;
new_node=(struct _tree_node *)malloc(sizeof(struct _tree_node));
new_node->value=data;
new_node->right=NULL;
new_node->left=NULL;
return new_node;
}
/************************************************************************************************
INORDER TRAVERSAL OF THE TREE
************************************************************************************************/
void inorder(struct _tree_node *toor)
{
if(toor == NULL)
return;
inorder(toor->left);
printf("%d ",toor->value);
inorder(toor->right);
}
/************************************************************************************************
METHOD COUNTS NUMBER OF NODES IN THE TREE
************************************************************************************************/
int count_tree_depth(struct _tree_node *toor)
{
if (toor==NULL)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = count_tree_depth(toor->left);
int rDepth = count_tree_depth(toor->right);
/* use the larger one */
if (lDepth > rDepth)
return(lDepth+1);
else
return(rDepth+1);
}
}
/************************************************************************************************
PREORDER TRAVERSAL OF THE TREE
************************************************************************************************/
void preorder(struct _tree_node *toor)
{
if(toor == NULL)
return;
printf("%d ",toor->value);
preorder(toor->left);
preorder(toor->right);
}
/************************************************************************************************
POSTORDER TRAVERSAL OF THE TREE
************************************************************************************************/
void postorder(struct _tree_node *toor)
{
if(toor == NULL)
return;
postorder(toor->left);
postorder(toor->right);
printf("%d ",toor->value);
}
/************************************************************************************************
METHOD DELETES THE COMPLETE BINARY TREE
************************************************************************************************/
void delete_complete_tree(struct _tree_node *toor)
{
if(toor == NULL)
return;
delete_complete_tree(toor->left);
delete_complete_tree(toor->right);
printf("\nNode Deleted :%d",toor->value);
toor->left=NULL;
toor->right=NULL;
free(toor);
}
/************************************************************************************************
FUNCTION RETURNS MAXIMUM FROM INORDER TRAVERSAL
************************************************************************************************/
struct _tree_node * inorder_max(struct _tree_node *toor)
{
int max=toor->value;
struct _tree_node *temp=NULL;
if(toor == NULL)
return;
inorder_max(toor->left);
if(max < toor->value)
{
max=toor->value;
temp=toor;
}
inorder_max(toor->right);
return temp;
}
/************************************************************************************************
METHOD FOR DELETING VALUE OR NODE FROM TREE
************************************************************************************************/
void delete_from_tree(struct _tree_node *toor,int data)
{
int flag=0;
struct _tree_node *temp,*prev,*temp1,*temp2;
__fpurge(stdin);
temp=toor;
while(temp != NULL)
{
if(temp->value < data)
{
prev=temp;
temp=temp->right;
flag=0;
}
else if(temp->value > data)
{
prev=temp;
temp=temp->left;
flag=0;
}
else
{
flag=1;
break;
}
}
if(flag == 1)
{
if((temp->right == NULL) && (temp->left == NULL))
{
if(prev->left == temp)
prev->left=NULL;
else
prev->right=NULL;
free(temp);
}
else if((temp->right == NULL) && (temp->left != NULL))
{
if(prev->left == temp)
prev->left=temp->left;
else
prev->right=temp->left;
free(temp);
}
else if((temp->right != NULL) && (temp->left == NULL))
{
if(prev->left == temp)
prev->left=temp->right;
else
prev->right=temp->right;
free(temp);
}
else
{
temp2=temp;
temp1=inorder_max(temp2);
temp->value=temp1->value;
delete_from_tree(temp1,temp1->value);
}
}
}
/************************************************************************************************
THIS METHOD CREATES A BINARY TREE
************************************************************************************************/
void create_tree(struct _tree_node *new_node)
{
struct _tree_node *temp,*prev;
__fpurge(stdin);
if(root==NULL)
{
root=new_node;
}
else
{
temp=root;
while(temp != NULL)
{ prev=temp;
if(temp->value < new_node->value)
temp=temp->right;
else
temp=temp->left;
}
if(prev->value < new_node->value)
prev->right=new_node;
else
prev->left=new_node;
}
}
/************************************************************************************************
THIS METHOD SEARCHES A VALUE IN THE TREE
************************************************************************************************/
int search_in_tree(int data)
{
int count=0,flag=0;
struct _tree_node *temp;
temp=root;
while(temp != NULL)
{
count++;
if(temp->value == data)
{
flag=1;
break;
}
else if(temp->value < data)
{
temp=temp->right;
flag=0;
}
else
{
temp=temp->left;
flag=0;
}
}
if(flag == 1)
return count;
else
return 0;
}
/***************************************************************** BINARY SEARCH TREE FUNTIONS ENDS **************************************************************************/
/************************************************************************************************
MAIN FUNCTION. IT IS THE STARTING POINT OF THE PROGRAM
************************************************************************************************/
int main()
{
struct _node *ptr;
struct _tree_node *tree_ptr;
int choiceNode,number_of_lists,num1,num2,number_of_trees,tree_choiceNode,data,value;
int choice,pos,s,flag=0,element,operation_choice,total_nodes;
char ch='y',chc='y',term,choice_list='y',program_continue='y',tree_choice='y',tree_ch='y',tree_choice_list='y';
do
{