forked from AishaDhiya/Data_Structure_lab_MCA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisjoint set.c
More file actions
128 lines (119 loc) · 2.34 KB
/
Disjoint set.c
File metadata and controls
128 lines (119 loc) · 2.34 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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
struct node *rep;
struct node *next;
int data;
}*heads[50],*tails[50];
static int countRoot=0;
void makeSet(int x){
struct node *new=(struct node *)malloc(sizeof(struct node));
new->rep=new;
new->next=NULL;
new->data=x;
heads[countRoot]=new;
tails[countRoot++]=new;
}
struct node* find(int a){
int i;
struct node *tmp=(struct node *)malloc(sizeof(struct node));
for(i=0;i<countRoot;i++){
tmp=heads[i];
while(tmp!=NULL){
if(tmp->data==a)
return tmp->rep;
tmp=tmp->next;
}
}
return NULL;
}
void unionSets(int a,int b){
int i,pos,flag=0,j;
struct node *tail2=(struct node *)malloc(sizeof(struct node));
struct node *rep1=find(a);
struct node *rep2=find(b);
if(rep1==NULL||rep2==NULL){
printf("Element not present in the DS\n");
return;
}
if(rep1!=rep2){
for(j=0;j<countRoot;j++){
if(heads[j]==rep2){
pos=j;
flag=1;
countRoot-=1;
tail2=tails[j];
for(i=pos;i<countRoot;i++){
heads[i]=heads[i+1];
tails[i]=tails[i+1];
}
}
if(flag==1)
break;
}
for(j=0;j<countRoot;j++){
if(heads[j]==rep1){
tails[j]->next=rep2;
tails[j]=tail2;
break;
}
}
while(rep2!=NULL){
rep2->rep=rep1;
rep2=rep2->next;
}
}
}
void main(){
int choice,x,i,j,y,flag=0;
printf("Menu\n1.Make Set\n2.Display set representatives\n3.Union\n4.Find Set\n5.Exit\n");
do{
printf("Enter your choice ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter new element");
scanf("%d",&x);
for(i=0;i<countRoot;i++){
flag=0;
if(heads[i]->data==x){
printf("Element already present in the disjoint set DS\n");
flag=1;
break;
}
}
if(flag==0)
makeSet(x);
break;
case 2:
for(i=0;i<countRoot;i++)
printf("%d ",heads[i]->data);
printf("\n");
break;
case 3:
printf("Enter first element");
scanf("%d",&x);
printf("Enter second element");
scanf("%d",&y);
unionSets(x,y);
break;
case 4:
printf("Enter the element");
scanf("%d",&x);
struct node *rep=(struct node *)malloc(sizeof(struct node));
rep=find(x);
if(rep==NULL)
printf("Element not present in the DS\n");
else
printf("The representative of %d is %d\n",x,rep->data);
break;
case 5:
exit(0);
default:
printf("Wrong choice\n");
break;
}
}
while(1);
};