当前位置:   article > 正文

C语言实现的一个哈希表_c语言中hash table头文件

c语言中hash table头文件

此哈希表中键和值的类型都是整数

1、哈希表实现的头文件hash.h:

头文件中定义了哈希表中哈希节点和哈希表结构体类型,以及此种哈希表的操作函数:

  1. #ifndef __HASH_INC__
  2. #define __HASH_INC__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define HASH_SIZE 1093
  7. /*
  8. 定义一个哈希表节点的结构体
  9. 成员变量:键,值,以及此节点的下一个节点的指针
  10. */
  11. typedef struct _hashnode {
  12. int key;
  13. int value;
  14. struct _hashnode * nextNode;
  15. }HashNode;
  16. /*
  17. 定义一个哈希表结构体
  18. 成员变量: 哈希表中节点总数,哈希节点指针数组
  19. */
  20. typedef struct _hashtable{
  21. int totalNodes;
  22. HashNode ** pphashNodes;
  23. }HashTable;
  24. int createHashTable(HashTable ** pphashTable);
  25. HashNode * getNodeHashTable(HashTable * pHashTable, int key);
  26. int insertHashTable(HashTable * pHashTable, int key, int value);
  27. int getValueHashTable(HashTable * pHashTable, int key, int * pValue);
  28. int deleteKeyHashTable(HashTable * pHashTable, int key);
  29. int deleteAllHashTable(HashTable ** ppHashTable);
  30. #endif

2、此处哈希表实现的源文件hash.c:

每个函数的作用和使用方法在函数定义中详细说明:

  1. #include "hash.h"
  2. /*
  3. 创建一个哈希表,由* pphashTable返回,返回0表示创建成功,-1表示创建失败
  4. */
  5. int createHashTable(HashTable ** pphashTable)
  6. {
  7. // 创建一个哈希表结构体
  8. (* pphashTable) = (HashTable *)malloc(sizeof(HashTable));
  9. if(NULL == (*pphashTable)){
  10. return -1;
  11. }
  12. // 当前节点总数为0
  13. // 分配一个大小为HASH_SIZE的节点指针数组,并初始化这些指针指向NULL
  14. (*pphashTable)->totalNodes = 0;
  15. (*pphashTable)->pphashNodes = (HashNode **)malloc(sizeof(HashNode *) * HASH_SIZE);
  16. if (NULL == (*pphashTable)->pphashNodes){
  17. return -1;
  18. }
  19. memset((*pphashTable)->pphashNodes, 0x00, sizeof(HASH_SIZE) * sizeof(HashNode *));
  20. return 0;
  21. }
  22. // 用键,值向哈希表中插入
  23. // 如果以key为键的节点已经存在,则改变对应的值。
  24. // 计算节点指针索引
  25. // 分配哈希节点,将传入的key和value写入对应的成员变量
  26. // 如果由key计算出的节点指针索引的指针指向空,则将以上哈希节点的地址存入这个指针
  27. // 如果指针索引的指针不为空,则从当前指向节点中nextNode寻找下一个节点,直接这个节点的nextNode指向空,
  28. // 将新分配节点地址存入当前节点的nextNode。
  29. int insertHashTable(HashTable * pHashTable, int key, int value)
  30. {
  31. int ikey = key % HASH_SIZE;
  32. HashNode * tmpNode = NULL;
  33. tmpNode = getNodeHashTable(pHashTable, key);
  34. // tmpNode = (HashNode *)malloc(sizeof(HashNode));
  35. if (tmpNode != NULL){
  36. tmpNode->value = value;
  37. return -1;
  38. }
  39. HashNode * newNode = (HashNode *)malloc(sizeof(HashNode));
  40. if (NULL == newNode){
  41. return -1;
  42. }
  43. newNode->key = key;
  44. newNode->value = value;
  45. newNode->nextNode = NULL;
  46. if (pHashTable->pphashNodes[ikey] == NULL){
  47. pHashTable->pphashNodes[ikey] = newNode;
  48. }
  49. else{
  50. tmpNode = pHashTable->pphashNodes[ikey];
  51. while (tmpNode->nextNode != NULL){
  52. tmpNode = tmpNode->nextNode;
  53. }
  54. tmpNode->nextNode = newNode;
  55. }
  56. pHashTable->totalNodes++;
  57. return 0;
  58. }
  59. /* 在哈希表中寻找以key为键的值,如果找到则用*pValue取回,并且函数返回0,否则函数返回-1 */
  60. int getValueHashTable(HashTable * pHashTable, int key, int * pValue){
  61. int ikey = key % HASH_SIZE;
  62. HashNode * tmpNode;
  63. if (pHashTable == NULL || pHashTable->totalNodes == 0 || pHashTable->pphashNodes[ikey] == NULL){
  64. return -1;
  65. }
  66. tmpNode = pHashTable->pphashNodes[ikey];
  67. while (tmpNode != NULL && tmpNode->key != key){
  68. tmpNode = tmpNode->nextNode;
  69. }
  70. if (tmpNode == NULL){
  71. return -1;
  72. }
  73. else{
  74. * pValue = tmpNode->value;
  75. return 0;
  76. }
  77. }
  78. /*
  79. 在哈希表中寻找以key为键的哈希节点,如果找到,则返回这个节点,否则,返回NULL
  80. */
  81. HashNode * getNodeHashTable(HashTable * pHashTable, int key)
  82. {
  83. int ikey = key % HASH_SIZE;
  84. HashNode * tmpNode = NULL;
  85. if (pHashTable == NULL || pHashTable->totalNodes == 0 || pHashTable->pphashNodes[ikey] == NULL){
  86. return NULL;
  87. }
  88. tmpNode = pHashTable->pphashNodes[ikey];
  89. while (tmpNode != NULL && tmpNode->key != key){
  90. tmpNode = tmpNode->nextNode;
  91. }
  92. return tmpNode;
  93. }
  94. /* 在哈希表中,删除以key为键的哈希节点,如果删除成功返回0,否则返回-1 */
  95. int deleteKeyHashTable(HashTable * pHashTable, int key)
  96. {
  97. int ikey = key % HASH_SIZE;
  98. HashNode * tmpNode, * prevNode;
  99. if (pHashTable == NULL || pHashTable->totalNodes == 0 || pHashTable->pphashNodes[ikey] == NULL){
  100. return -1;
  101. }
  102. tmpNode = pHashTable->pphashNodes[ikey];
  103. while (tmpNode && tmpNode->key != key){
  104. prevNode = tmpNode;
  105. tmpNode = tmpNode->nextNode;
  106. }
  107. if (tmpNode == NULL){
  108. return -1;
  109. }
  110. else{
  111. if (tmpNode == pHashTable->pphashNodes[ikey]){
  112. pHashTable->pphashNodes[ikey] = tmpNode->nextNode;
  113. }
  114. else{
  115. prevNode->nextNode = tmpNode->nextNode;
  116. }
  117. free(tmpNode);
  118. pHashTable->totalNodes--;
  119. return 0;
  120. }
  121. }
  122. /* 释放哈希表中所有哈希节点以及哈希表 */
  123. int deleteAllHashTable(HashTable ** ppHashTable)
  124. {
  125. int i;
  126. HashNode * tmpNode, * nextNode;
  127. if ((* ppHashTable) == NULL || (*ppHashTable)->totalNodes == 0)
  128. {
  129. return -1;
  130. }
  131. for (i = 0; i < HASH_SIZE; i++){
  132. if ((*ppHashTable)->pphashNodes[i] == NULL){
  133. continue;
  134. }
  135. else{
  136. tmpNode = (*ppHashTable)->pphashNodes[i];
  137. while (tmpNode->nextNode != NULL){
  138. nextNode = tmpNode->nextNode;
  139. free(tmpNode);
  140. (*ppHashTable)->totalNodes--;
  141. tmpNode = nextNode;
  142. }
  143. free(tmpNode);
  144. (*ppHashTable)->totalNodes--;
  145. (*ppHashTable)->pphashNodes[i] = NULL;
  146. if ((*ppHashTable)->totalNodes == 0){
  147. break;
  148. }
  149. }
  150. }
  151. free(* ppHashTable);
  152. (*ppHashTable) = NULL;
  153. return 0;
  154. }

3、测试程序test.c如下:

  1. #include <stdio.h>
  2. #include "hash.h"
  3. void test(int key1, int value1, int key2, int value2)
  4. {
  5. HashTable * pHashTable;
  6. /* create hash table */
  7. if (createHashTable(&pHashTable) != 0){
  8. printf("create failed\n");
  9. return;
  10. }
  11. /* insert key and value into hash table */
  12. insertHashTable(pHashTable, key1, value1);
  13. insertHashTable(pHashTable, key2, value2);
  14. printf("current total nodes:%d\n", pHashTable->totalNodes);
  15. /* get hashnode with key*/
  16. HashNode * tmpNode = getNodeHashTable(pHashTable, key1);
  17. printf("Get Hashnode with key:\n");
  18. if (tmpNode != NULL){
  19. printf("key:%d--->value:%d\n", key1, tmpNode->value);
  20. }
  21. else{
  22. printf("key:%d-->no value in hash table\n",key1);
  23. }
  24. tmpNode = getNodeHashTable(pHashTable, key2);
  25. if (tmpNode != NULL){
  26. printf("key:%d--->value:%d\n", key2, tmpNode->value);
  27. }
  28. else{
  29. printf("key:%d-->no value in hash table\n",key2);
  30. }
  31. /* get value with key*/
  32. printf("Get value with key:\n");
  33. int value = 0;
  34. getValueHashTable(pHashTable, key1, &value);
  35. printf("key:%d--->value:%d\n", key1, value);
  36. getValueHashTable(pHashTable, key2, &value);
  37. printf("key:%d--->value:%d\n", key2, value);
  38. /* delete hashnode with key */
  39. deleteKeyHashTable(pHashTable, key1);
  40. printf("current totoal nodes:%d\n", pHashTable->totalNodes);
  41. tmpNode = getNodeHashTable(pHashTable, key1);
  42. if (tmpNode != NULL){
  43. printf("key:%d--->value:%d\n", key1, tmpNode->value);
  44. }
  45. else{
  46. printf("key:%d-->no value in hash table\n",key1);
  47. }
  48. tmpNode = getNodeHashTable(pHashTable, key2);
  49. if (tmpNode != NULL){
  50. printf("key:%d--->value:%d\n", key2, tmpNode->value);
  51. }
  52. else{
  53. printf("key:%d-->no value in hash table\n",key2);
  54. }
  55. /* delete all hashnodes and hashtable */
  56. deleteAllHashTable(&pHashTable);
  57. }
  58. int main()
  59. {
  60. test(1,11,2,22);
  61. return 0;
  62. }

4、用于编译的Makefile文件:

  1. OBJS=$(patsubst %.c,%.o, $(wildcard ./*c))
  2. TARGET=main
  3. $(TARGET):$(OBJS)
  4. $(CXX) $^ -o $(TARGET)
  5. %.o:%.c
  6. $(CXX) -c $^ -o $@
  7. clean:
  8. rm -rf *.o $(TARGET)
  9. .PHONY:clean

5、执行make进行编译:

  1. orangepi@orangepi5:~/C_program/hash_dir$ make
  2. g++ -c hash.c -o hash.o
  3. g++ -c test.c -o test.o
  4. g++ hash.o test.o -o main
  5. orangepi@orangepi5:~/C_program/hash_dir$ ls
  6. hash.c hash.c.bak hash.h hash.o main Makefile test.c test.o

6、测试生成的可执行程序main:

  1. orangepi@orangepi5:~/C_program/hash_dir$ ./main
  2. current total nodes:2
  3. Get Hashnode with key:
  4. key:1--->value:11
  5. key:2--->value:22
  6. Get value with key:
  7. key:1--->value:11
  8. key:2--->value:22
  9. current totoal nodes:1
  10. key:1-->no value in hash table
  11. key:2--->value:22

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

闽ICP备14008679号