生活随笔
收集整理的這篇文章主要介紹了
数据结构源码笔记(C语言):集合的位向量表示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <stdio.h>
#include <stdlib.h>
#define superNum 20
#define sonNum 8
typedef struct
{int size
; char * array
;
} BitSet
;
BitSet
* createEmptySet
(int n
)
{ int i
;BitSet
* s
= (BitSet
*)malloc(sizeof(BitSet
));if (s
!=NULL){s
->size
= (n
+ 7) / 8;s
->array
= (char *)malloc(s
->size
* sizeof(char));if (s
->array
!= NULL){for (i
= 0; i
< s
->size
; i
++) s
->array
[i
] = '\0';return s
; }}return NULL;
}
int insert
(BitSet
* s
, int index
)
{ if (index
>= 0 && index
>>3 < s
->size
) {s
->array
[index
>> 3] |= (1 << (index
& 07)); return 1; } return 0;
}
int delete(BitSet
* s
, int index
)
{ if (index
>= 0 && index
>> 3 < s
->size
){s
->array
[index
>> 3] &= ~(1 << (index
& 07)); return 1;}return 0;
}
int member(BitSet
* s
, int index
)
{ if (index
>= 0 && index
>> 3 < s
->size
&&(s
->array
[index
>> 3] & (1 << (index
& 07))))return 1;return 0;
}
int union_set(BitSet
*s0
,BitSet
*s1
,BitSet
*s2
)
{
int i
;if (s0
->size
!= s1
->size
|| s2
->size
!= s1
->size
) return 0;for (i
= 0; i
< s1
->size
; i
++)s2
->array
[i
]=s0
->array
[i
] | s1
->array
[i
];return 1;
}
int intersection(BitSet
* s0
, BitSet
* s1
, BitSet
* s2
) {
int i
;if (s0
->size
!= s1
->size
|| s2
->size
!= s1
->size
) return 0;for (i
= 0; i
< s1
->size
; i
++)s2
->array
[i
]=s0
->array
[i
] & s1
->array
[i
];return 1;
}
int difference(BitSet
* s0
, BitSet
* s1
, BitSet
* s2
)
{
int i
;if (s0
->size
!= s1
->size
|| s2
->size
!= s1
->size
) return 0;for (i
= 0; i
< s1
->size
; i
++)s2
->array
[i
]=s0
->array
[i
] & ~s1
->array
[i
];return 1;
}void display(BitSet
*s
,int * super_set
)
{int i
,j
;char ch
;printf("{");for (i
=0; i
<s
->size
; i
++) for(j
=0;j
<8;j
++){ ch
=s
->array
[i
]>>j
;if(ch
%2!=0) printf("%d ",super_set
[i
*8+j
]); } printf("}\n\n");}int main()
{BitSet
*s0
,*s1
,*s2
,*s3
,*s4
;int i
=0,j
=0;int superset
[superNum
]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};int subset
[sonNum
]={1,2,4,5,7,8,10,19};int othersubset
[4]={5,11,14,17};s0
=createEmptySet
(superNum
);s1
=createEmptySet
(superNum
);s2
=createEmptySet
(superNum
);s3
=createEmptySet
(superNum
);s4
=createEmptySet
(superNum
);for(i
=0;i
<superNum
;i
++){while(j
<sonNum
){if(subset
[j
]==superset
[i
]) {insert(s0
, i
);break;}j
++;}; j
=0; }printf("集合S0=");display(s0
,superset
);printf("刪除元素后,集合S0=");delete(s0
,3);display(s0
,superset
);if(member(s0
, 11))printf("判斷11是該集合的元素\n");else printf("判斷11不是該集合的元素\n");j
=0;for(i
=0;i
<superNum
;i
++){while(j
<4){if(othersubset
[j
]==superset
[i
]) {insert(s1
, i
);break;}j
++;}; j
=0; }printf("集合S1=");display(s1
,superset
);union_set
(s0
, s1
, s2
);printf("s0,s1做并后,s2=");display(s2
,superset
);intersection(s0
, s1
, s3
);printf("s0,s1做交后,s3=");display(s3
,superset
);difference(s0
, s1
, s4
);printf("s0,s1做差后,s4=");display(s4
,superset
);return 0;
}
數據結構源碼筆記(C語言描述)匯總:
數據結構源碼筆記(C語言):英文單詞按字典序排序的基數排序
數據結構源碼筆記(C語言):直接插入排序
數據結構源碼筆記(C語言):直接選擇排序
數據結構源碼筆記(C語言):置換-選擇算法
數據結構源碼筆記(C語言):Huffman樹字符編碼
數據結構源碼筆記(C語言):Josephus問題之順序表
數據結構源碼筆記(C語言):Josephus問題之循環鏈接表
數據結構源碼筆記(C語言):多項式合并
數據結構源碼筆記(C語言):二叉樹之葉子結點旋轉銷毀
數據結構源碼筆記(C語言):哈夫曼樹
數據結構源碼筆記(C語言):集合的位向量表示
數據結構源碼筆記(C語言):鏈接隊列
數據結構源碼筆記(C語言):鏈接棧
數據結構源碼筆記(C語言):線性表的單鏈表示
數據結構源碼筆記(C語言):線性表的順序表示
數據結構源碼筆記(C語言):棧的基本操作
數據結構源碼筆記(C語言):中綴表達式
數據結構源碼筆記(C語言):希爾插入排序
數據結構源碼筆記(C語言):索引文件建立和查找
數據結構源碼筆記(C語言):冒泡排序
數據結構源碼筆記(C語言):快速排序
數據結構源碼筆記(C語言):可變長度字符串的快速排序
數據結構源碼筆記(C語言):基數排序
數據結構源碼筆記(C語言):二路歸并排序
數據結構源碼筆記(C語言):堆排序
數據結構源碼筆記(C語言):二叉樹搜索樹Kruskal
數據結構源碼筆記(C語言):二叉搜索樹Prim
數據結構源碼筆記(C語言):最短路徑弗洛伊德算法
數據結構源碼筆記(C語言):深度、廣度優先生成樹
數據結構源碼筆記(C語言):鄰接矩陣轉化鄰接表
數據結構源碼筆記(C語言):統計字符串中出現的字符及其次數
數據結構源碼筆記(C語言):順序查找
數據結構源碼筆記(C語言):哈希表的相關運算算法
數據結構源碼筆記(C語言):分塊法查找
數據結構源碼筆記(C語言):二分查找
數據結構源碼筆記(C語言):二叉樹遍歷
數據結構源碼筆記(C語言):二叉平衡樹的相關操作算法
數據結構源碼筆記(C語言):二叉排序樹的基本操作算法
數據結構源碼筆記(C語言):B樹的相關運算算法
總結
以上是生活随笔為你收集整理的数据结构源码笔记(C语言):集合的位向量表示的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。