当前位置:   article > 正文

Hash表 数据结构_针对某个集体中人名设计一个哈希表,哈希函数用除留余数法构造,用线性探测再散列法

针对某个集体中人名设计一个哈希表,哈希函数用除留余数法构造,用线性探测再散列法
Hash 表
key: value
key 保存信息的唯一标识
将char*类型的key转化为int型作为寻找信息的方式
假设有10000个人, 分成100个表保存信息, 每个表保存100个人

使用Hash算法对key做唯一标识计算

  1. // main.cpp
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "hashclass.h"
  6. int main(int argc, char* argv[]) {
  7. HashTab* t1 = createTab(10);
  8. insertHashElem(t1, "1", (void*)"河南");
  9. insertHashElem(t1, "2", (void*)"北京");
  10. insertHashElem(t1, "3", (void*)"天津");
  11. insertHashElem(t1, "13", (void*)"天津");
  12. insertHashElem(t1, "23", (void*)"天津");
  13. insertHashElem(t1, "33", (void*)"天津");
  14. insertHashElem(t1, "43", (void*)"天津");
  15. insertHashElem(t1, "4", (void*)"河北");
  16. insertHashElem(t1, "5", (void*)"安徽");
  17. char* addr = (char*)findHashElem(t1, "1");
  18. printf("%s\n", addr);
  19. resetHashElem(t1, "2", (void*)"China");
  20. addr = (char*)findHashElem(t1, "2");
  21. printf("%s\n", addr);
  22. deleteHashElem(t1, "23");
  23. if (addr = (char*)findHashElem(t1, "23")) {
  24. printf("%s\n", addr);
  25. }
  26. else {
  27. printf("没有");
  28. }
  29. clearHashElemList(t1);
  30. printf("%s\n", (char*)findHashElem(t1, "5"));
  31. system("pause");
  32. return 0;
  33. }
  1. // HashClass.cpp
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "HashClass.h"
  6. #define my_malloc malloc
  7. #define my_free free
  8. // 哈希算法
  9. static unsigned int
  10. algorimthHash(const char* key) {
  11. register unsigned int h;
  12. register unsigned char* p;
  13. for (h = 0, p = (unsigned char*)key; *p; p++) {
  14. h = (32 << 5)*h + *p;
  15. }
  16. return h;
  17. }
  18. struct HashNode {
  19. char* key;
  20. void* value;
  21. HashNode* next;
  22. };
  23. struct HashTab {
  24. int group;
  25. HashNode** Hash_sets;
  26. };
  27. HashTab* createTab(int n) {
  28. HashTab* tab = (HashTab*)my_malloc(sizeof(HashTab));
  29. memset(tab, 0, sizeof(HashTab));
  30. tab->Hash_sets = (HashNode**)my_malloc(sizeof(HashNode*)*n);
  31. memset(tab->Hash_sets, 0, sizeof(HashNode*)*n);
  32. tab->group = n;
  33. return tab;
  34. }
  35. void deleteTab(HashTab* tab) {
  36. clearHashElemList(tab);
  37. if (tab->Hash_sets) {
  38. free(tab->Hash_sets);
  39. tab->Hash_sets = NULL;
  40. }
  41. free(tab);
  42. }
  43. void insertHashElem(HashTab* tab, const char* key, void* value) {
  44. HashNode* node = (HashNode*)my_malloc(sizeof(HashNode));
  45. memset(node, 0, sizeof(HashNode));
  46. node->key = strdup(key);
  47. node->value = value;
  48. int g_index = algorimthHash(key) % tab->group;
  49. HashNode* head = tab->Hash_sets[g_index];
  50. node->next = head;
  51. tab->Hash_sets[g_index] = node;
  52. }
  53. void resetHashElem(HashTab* tab, const char* key, void* value) {
  54. int g_index = algorimthHash(key) % tab->group;
  55. HashNode** transNode = &tab->Hash_sets[g_index];
  56. while (*transNode) {
  57. if (strcmp((*transNode)->key, key) == 0) {
  58. (*transNode)->value = value;
  59. return;
  60. }
  61. transNode = &(*transNode)->next;
  62. }
  63. HashNode* node = (HashNode*)my_malloc(sizeof(HashNode));
  64. memset(node, 0, sizeof(HashNode));
  65. node->key = strdup(key);
  66. node->value = value;
  67. *transNode = node;
  68. }
  69. void* findHashElem(HashTab* tab, const char* key) {
  70. int g_index = algorimthHash(key) % tab->group;
  71. HashNode* transNode = tab->Hash_sets[g_index];
  72. while (transNode) {
  73. if (strcmp(transNode->key, key) == 0) {
  74. return transNode->value;
  75. }
  76. transNode = transNode->next;
  77. }
  78. return NULL;
  79. }
  80. void deleteHashElem(HashTab* tab, const char* key) {
  81. int g_index = algorimthHash(key) % tab->group;
  82. HashNode** transNode = &tab->Hash_sets[g_index];
  83. while (*transNode) {
  84. if (strcmp((*transNode)->key, key) == 0) {
  85. HashNode* node = *transNode;
  86. *transNode = (*transNode)->next;        // 将下一个链表节点接到上一个
  87. node->next = NULL;
  88. free(node->key);
  89. free(node);
  90. }
  91. else {
  92. transNode = &(*transNode)->next;        // 将链表节点后移一次,并将二级指针的值修改为链表后移前的值
  93. }
  94. }
  95. }
  96. void clearHashElemList(HashTab* tab) {
  97. for (int i = 0; i < tab->group; i++) {
  98. HashNode* transNode = tab->Hash_sets[i];
  99. tab->Hash_sets[i] = NULL;
  100. while (transNode) {
  101. HashNode* node = transNode;
  102. transNode = transNode->next;
  103. node->next = NULL;
  104. free(node->key);
  105. free(node);
  106. }
  107. }
  108. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/981063
推荐阅读
相关标签
  

闽ICP备14008679号