当前位置:   article > 正文

C语言哈希表_c 哈希表 标准库

c 哈希表 标准库

 

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 15
  5. typedef int datatype;
  6. typedef struct node {
  7. datatype key;
  8. datatype value;
  9. struct node * next;
  10. }listnode, *linklist;
  11. typedef struct {
  12. listnode data[N];
  13. }hash;
  14. hash * hash_create();
  15. int hash_insert(hash *HT, datatype key);
  16. linklist hash_search(hash *HT, datatype key);
  17. int main(int argc, const char *argv[])
  18. {
  19. hash * HT;
  20. int data[] = {23, 34, 14, 38, 46, 16, 68, 15, 7, 31, 26};
  21. int i;
  22. int key;
  23. linklist r;
  24. printf("hash[15] = %ld\n", sizeof(hash));
  25. printf("hash = %ld\n", sizeof(listnode));
  26. if ((HT = hash_create()) == NULL) {
  27. return -1;
  28. }
  29. for (i = 0; i < sizeof(data)/sizeof(int); i++) {
  30. hash_insert(HT, data[i]);
  31. }
  32. printf("input:");
  33. scanf("%d", &key);
  34. r = hash_search(HT, key);
  35. if (r == NULL)
  36. printf("not found\n");
  37. else
  38. printf("found:%d %d\n", key, r->value);
  39. return 0;
  40. }
  41. hash * hash_create() {
  42. hash * HT;
  43. printf("HT = %ld\n", sizeof(HT));
  44. if ((HT = (hash *)malloc(sizeof(hash))) == NULL) {
  45. printf("malloc failed\n");
  46. return NULL;
  47. }
  48. memset(HT, 0, sizeof(hash));
  49. return HT;
  50. }
  51. int hash_insert(hash *HT, datatype key) {
  52. linklist p, q;
  53. if (HT == NULL) {
  54. printf("HT is NULL\n");
  55. return -1;
  56. }
  57. if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
  58. printf("malloc failed\n");
  59. return -1;
  60. }
  61. p->key = key;
  62. p->value = key % N;
  63. p->next = NULL;
  64. q = &(HT->data[key % N]); //得到我们的哪一个桶
  65. while (q->next && q->next->key < p->key ) {
  66. q = q->next;
  67. }
  68. p->next = q->next;
  69. q->next = p;
  70. return 0;
  71. }
  72. linklist hash_search(hash *HT, datatype key) {
  73. linklist p;
  74. if (HT == NULL) {
  75. printf("HT is NULL\n");
  76. return NULL;
  77. }
  78. p = &(HT->data[key % N]); //得到哪一个桶
  79. while (p->next && p->next->key != key) {
  80. p = p->next;
  81. }
  82. if (p->next == NULL) {
  83. return NULL;
  84. } else {
  85. printf("found\n");
  86. return p->next;
  87. }
  88. }

运行结果:

嵌入式技术公开课的哈希查找(线性探测法),比较好理解

  1. #include <stdio.h>
  2. #define SIZE 12
  3. #define M 15
  4. int hash(int key);
  5. int insert_hashtable(int HT[], int key);
  6. int line_detect(int HT[], int H0, int key);
  7. void show_hashtable(int HT[], int m);
  8. int search_hashtable(int HT[], int key);
  9. int main(void)
  10. {
  11. int num, result;
  12. int arr[SIZE] = {14, 36, 42, 38, 40, 15, 19, 12, 51, 65, 34, 25};
  13. int HT[M] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  14. for(int i = 0; i < SIZE; i++)
  15. {
  16. if(!insert_hashtable(HT, arr[i]))
  17. {
  18. printf("Failed to create Hash Table.\n");
  19. return 0;
  20. }
  21. }
  22. printf("Show Hash Table:\n");
  23. show_hashtable(HT, M);
  24. printf("Please enter a number you want to find in Hash Table:\n");
  25. scanf("%d", &num);
  26. result = search_hashtable(HT, num);
  27. if(result != -1)
  28. printf("Find %d int position %d.\n", num, result);
  29. else
  30. printf("Cannot find %d in Hash Table.\n", num);
  31. return 0;
  32. }
  33. int insert_hashtable(int HT[], int key)
  34. {
  35. int index = hash(key);
  36. int Hi = -1;
  37. if(HT[index] == -1) /* 我们通过index找到里面的key, 如果等于-1,则里面没存,我们把key存进去 */
  38. {
  39. HT[index] = key;
  40. return 1;
  41. }
  42. else /* 里面不是-1,则冲突了 */
  43. {
  44. Hi = line_detect(HT, index, key); /* 解决冲突问题 */
  45. if(Hi != -1)
  46. {
  47. HT[Hi] = key;
  48. return 1;
  49. }
  50. }
  51. return 0;
  52. }
  53. /* 通过哈希函得到我们的存储数据的位置index */
  54. int hash(int key)
  55. {
  56. return key % 13; /* 为啥是%13, 不大于M的最大质数,我们M为15,上面写了 */
  57. }
  58. /* 线性探测法解决冲突问题 */
  59. int line_detect(int HT[], int H0, int key)
  60. {
  61. int Hi;
  62. for(int di = 1; di <= M; di++)
  63. {
  64. /* (key + di) % M 除M取余,而不是除质数取余 */
  65. Hi = (H0 + di) % M;
  66. if(HT[Hi] == -1) /* 解决冲突问题 */
  67. return Hi;
  68. else if(HT[Hi] == key) /* 为了查找的时候找到位置 */
  69. return Hi;
  70. }
  71. return -1;
  72. }
  73. void show_hashtable(int HT[], int m)
  74. {
  75. for(int i = 0; i < m; i++)
  76. printf("%d\t", HT[i]);
  77. printf("\n");
  78. }
  79. int search_hashtable(int HT[], int key)
  80. {
  81. int H0 = hash(key);
  82. int Hi;
  83. if(HT[H0] == -1)
  84. return -1;
  85. else if(HT[H0] == key)
  86. return H0;
  87. else
  88. {
  89. Hi = line_detect(HT, H0, key); /* 线性探测法,解决冲突问题 */
  90. if(HT[Hi] == key)
  91. return Hi;
  92. else
  93. return -1;
  94. }
  95. }

运行结果:

链式哈希的引入?

你们不是都喜欢同一个位置,那就把你们串起来,存在同一个位置。

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define SIZE 12
  4. #define M 15
  5. struct node
  6. {
  7. unsigned int elem;
  8. struct node *next;
  9. };
  10. void insert_hashtable(struct node *HT[], int key);
  11. int hash(int key);
  12. void show_hashtable(struct node *HT[], int m);
  13. int search_hashtable(struct node *HT[], int key);
  14. int main(void)
  15. {
  16. int num, result;
  17. int arr[SIZE] = {14, 36, 42, 38, 40, 15, 19, 12, 51, 65, 34, 25};
  18. struct node *HT[M] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  19. for(int i = 0; i < SIZE; i++)
  20. {
  21. insert_hashtable(HT, arr[i]);
  22. }
  23. printf("Show Hash Table:\n");
  24. show_hashtable(HT, M);
  25. printf("Please enter a number you want to find in Hash Table:\n");
  26. scanf("%d", &num);
  27. result = search_hashtable(HT, num);
  28. if(result == 1)
  29. printf("Find %d in Link Hash Table.\n", num);
  30. else
  31. printf("Cannot find %d in Link Hash Table.\n", num);
  32. return 0;
  33. }
  34. void insert_hashtable(struct node *HT[], int key)
  35. {
  36. int index = hash(key);
  37. struct node *p = (struct node *)malloc(sizeof(struct node));
  38. p->elem = key;
  39. p->next = HT[index];
  40. HT[index] = p;
  41. }
  42. int hash(int key)
  43. {
  44. return key % 13;
  45. }
  46. void show_hashtable(struct node *HT[], int m)
  47. {
  48. struct node *p;
  49. for(int i = 0; i < m; i++)
  50. {
  51. printf("Hash Table index %d has: ", i);
  52. for(p = HT[i]; p != NULL; p = p->next)
  53. printf("%d ", p->elem);
  54. printf("\n");
  55. }
  56. }
  57. int search_hashtable(struct node *HT[], int key)
  58. {
  59. struct node *p;
  60. int index = hash(key);
  61. for(p = HT[index]; p != NULL; p = p->next)
  62. {
  63. if(p->elem == key)
  64. return 1;
  65. }
  66. return 0;
  67. }

运行结果:

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

闽ICP备14008679号