集合的表示
采用数组存储形式
typedef struct{
ElementType Data;
int Parent;
}SetType;
集合运算
1、查找某元素所在的集合
int Find(SetType S[],ElementType X){
int i;
for(i=0;i<MaxSize && S[i].Data != X; i++);
if(i>=MaxSize){
return -1;
}
for(;S[i].Parent>=0;i=S[i].Parent);
return i;
}
2、并运算
void Union(SetType S[],ElementType X1,ElementType X2){
int Root1,Root2;
Root1=Find(S,X1);
Root2=Find(S,X2);
if(Root1 != Root2){
S[Root2].Parent = Root1;
}
}
本文详细介绍了集合的数组存储形式及其实现,并提供了查找元素所在集合与并集运算的具体算法。通过SetType结构体定义集合,使用Find函数进行元素查找,Union函数实现集合的并运算。
4314

被折叠的 条评论
为什么被折叠?



