c语⾔实现通⽤数据结构:通⽤集合(HashSet)注意集合中只存储了指针,没有储存实际的数据。
对于新的数据类型来说,需要⾃定义HashCode函数和equal函数。
下⾯还给出了⼏个常见的hashCode函数和equal函数。
c语言listinsert函数(1)HashCode函数
头⽂件
源⽂件[cpp]
01. /*************************
02. *** File myHashCode.h
03. **************************/
04. #ifndef MYHASHCODE_H_INCLUDED
05. #define MYHASHCODE_H_INCLUDED
06.
07. #include <string.h>
08.
09. #define HASHCODE_MULT 31
10.
11. //默认的hashCode
12. int myHashCodeDefault(void * a);
13.
14. //int类型hashCode
15. int myHashCodeInt(void * a);
16.
17. //char类型的hashCode
18. int myHashCodeChar(void * a);
19.
20. //string类型的hashCode
21. int myHashCodeString(void * a);
22.
23. #endif // MYHASHCODE_H_INCLUDED
(2)equal 函数
头⽂件
源⽂件01. /************************* 02. *** File myHashCode.c 03. **************************/ 04. #include "myHashCode.h" 05. 06. //默认的hashCode 07. int myHashCodeDefault(void * a) 08. { 09. return (int ) a; 10. } 11. 12. //int 类型hashCode 13. int myHashCodeInt(void * a) 14. { 15.
int * aa = (int *) a; 16. return *aa; 17. } 18. 19. //char 类型的hashCode 20. int myHashCodeChar(void * a) 21. { 22. char *aa = (char *) a; 23. return *aa; 24. } 25. 26. //string 类型的hashCode 27. int myHashCodeString(void * a) 28. { 29. int re = 0; 30. char *aa = (char *) a; 31. while (*aa) 32. { 33. re += HASHCODE_MULT * *aa; 34. aa++; 35. } 36. return re; 37. }
[cpp]
01. /************************* 02. *** File myEqual.h 03. **************************/ 04. #ifndef MYEQUAL_H_INCLUDED 05. #define MYEQUAL_H_INCLUDED 06. 07. //默认的相等的⽅法 08. int myEqualDefault(void * a, void *b); 09. 10. //int 类型相等的⽅法 11. int myEqualInt(void * a, void *b); 12. 13. //char 类型相等的⽅法 14. int myEqualChar(void * a, void *b); 15. 16. //string 类型相等的⽅法 17. int myEqualString(void * a, void *b); 18. 19. #endif // MYEQUAL_H_INCLUDED
(3)HashSet
头⽂件01. /************************* 02. *** File myEqual.c 03. **************************/ 04. #include "myEqual.h" 05. #include <string.h> 06. 07. //默认的相等的⽅法 08. int myEqualDefault(void * a,
void *b) 09. { 10. return a == b; 11. } 12. 13. //int 类型相等的⽅法 14. int myEqualInt(void * a, void *b) 15. { 16. int *aa = (int *) a; 17. int *bb = (int *) b; 18. return *aa == *bb; 19. } 20. 21. //char 类型相等的⽅法 22. int myEqualChar(void * a, void *b) 23. { 24. char *aa = (char *) a; 25. char *bb = (char *) b; 26. return *aa = *bb; 27. } 28. 29. //string 类型相等的⽅法 30. int myEqualString(void * a, void *b) 31. { 32. char *aa = (char *) a; 33. char *bb = (char *) b; 34. return strcmp(aa, bb)==0; 35. }
源⽂件01. #ifndef MYHASHSET_H_INCLUDED 02. #define MYHASHSET_H_INCLUDED 03. 04. # include "myHashMap.h" 05. 06. typedef struct myHashSet 07. { 08. int size; //⼤⼩ 09. int initialCapacity; //初始容量 10. float loadFactor; //加载因⼦ 11. int (*hashCode)(void *data); 12. int (*equal)(void *data1, void *data2); 13. MyList ** dataList; 14. } MyHashSet; 15. 16. typedef struct myHashSetIterator 17. { 18. int index; //第⼏个链表 19. MyHashSet *set; 20. MyNode *current; 21. int count; //第⼏个数据 22. } MyHashSetIterator; 23. 24. //创建HashSet 25.
MyHashSet *createMyHashSet(int (*hashCode)(void *data),int (*equal)(void *data1,void *data2)); 26. 27. //使⽤全部参数创建HashSet 28.
MyHashSet *createMyHashSetForAll(int initialCapacity,float loadFactor,int (*hashCode)(void *data),int (*equal)(void *data1,void *data2)); 29. 30. //释放HashSet 31. void freeMyHashSet(MyHashSet * set); 32. 33. //是否包含某个data 34. int myHashSetContains(MyHashSet * const set, void * const data); 35. 36. //增加⼀条数据,返回是否添加成功 37. int myHashSetAddData(MyHashSet * const set, void * const data); 38. 39. //数据的容量 40. int myHashSetGetSize(const MyHashSet * const set); 41. 42. //创建迭代器 43. MyHashSetIterator* createMyHashSetIterator(MyHashSet * const set); 44. 45. //释放迭代器 46. void freeMyHashSetIterator(MyHashSetIterator* iterator); 47. 48. //迭代器是否有下⼀个 49. int myHashSetIteratorHasNext(MyHashSetIterator* iterator); 50. 51. //遍历下⼀个元素 52. void * myHashSetIteratorNext(MyHashSetIterator* iterator); 53. 54. //删除⼀条数据,返回是否删除成功 55. int myHashSetRemoveData(MyHashSet * const set, void * const data); 56. 57. //将第⼆个Set 的全部对象加⼊到第⼀个,返回增加的个数 58. int myHashSetAddAllSet(MyHashSet * set,MyHashSet *other); 59. 60. //复制HashSet 61. MyHashSet* myHashSetCopy(MyHashSet * set); 62. 63. //遍历 64. void myHashSetOutput(MyHashSet *set, void (*pt)(void *)); 65. 66. #endif // MYHASHSET_H_INCLUDED [cpp]
01. # include "myHashSet.h" 02. #include <stdlib.h> 03. //创建HashSet 04. MyHashSet *createMyHashSet(int (*hashCode)(void *data), int (*equal)
04. MyHashSet *createMyHashSet(int(*hashCode)(void *data), int(*equal)
(void *data1, void *data2))
05. {
06. MyHashSet *re = malloc(sizeof(MyHashSet));
07. re->size = 0;
08. re->loadFactor = DEFAULT_LOAD_FACTOR;
09. re->initialCapacity = DEFAULT_INITIAL_CAPACITY;
10. re->dataList = (MyList **) malloc(sizeof(MyList*) * re->initialCapacity);
11. re->hashCode = hashCode;
12. re->equal = equal;
13. for (int i = 0; i < re->initialCapacity; i++)
14. {
15. re->dataList[i] = createMySearchList(equal);
16. }
17. return re;
18. }
19.
20. //使⽤全部参数创建HashSet
21. MyHashSet *createMyHashSetForAll(int initialCapacity, float loadFactor, int(*hashCode)
(void *data), int(*equal)(void *data1, void *data2))
22. {
23. MyHashSet *re = createMyHashSet(hashCode, equal);
24. re->initialCapacity = initialCapacity;
25. re->loadFactor = loadFactor;
26. return re;
27. }
28.
29. //释放HashSet
30. void freeMyHashSet(MyHashSet * set)
31. {
32. for (int i = 0; i < set->initialCapacity; i++)
33. {
34. freeMyList(set->dataList[i]);
35. }
36. free(set->dataList);
37. free(set);
38. }
39.
40. //是否包含某个data
41. int myHashSetContains(MyHashSet * const set, void * const data)
42. {
43. int hasCode = (*(set->hashCode))(data);
44. hasCode %= set->initialCapacity;
45. if (hasCode<0)
46. hasCode+=set->initialCapacity;
47. int re = myListFindDataIndex(set->dataList[hasCode], data);
48. return re > -1;
49. }
50.
51. void rebuildMyHashSet(MyHashSet * set)
52. {
53. int newSize = set->initialCapacity * 2;
54. MyList **newdataList = (MyList **) malloc(sizeof(MyList*) * newSize);
55. for (int i = 0; i < newSize; i++)
56. {
57. newdataList[i] = createMyList();
58. }
59. MyHashSetIterator* it = createMyHashSetIterator(set);
60. while (myHashSetIteratorHasNext(it))
61. {
62. void * data = myHashSetIteratorNext(it);
63. int hasCode = (*(set->hashCode))(data);
64. hasCode %= newSize;
65. myListInsertDataAtLast(newdataList[hasCode], data);
66. }
67. freeMyHashSetIterator(it);
68. for (int i = 0; i < set->initialCapacity; i++)
69. {
70. freeMyList(set->dataList[i]);
71. }
72. free(set->dataList);
73. set->dataList = newdataList;
74. set->initialCapacity = newSize;
75. }
76.
77. //增加⼀条数据,返回是否添加成功
78. int myHashSetAddData(MyHashSet * const set, void * const data)
79. {
80. int hasCode = (*(set->hashCode))(data);
81. hasCode %= set->initialCapacity;
82. if (hasCode<0)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论