用c实现HASH表创建、插入、查找、删除、打印(欢迎高手指点)【转】

  1. /************************************************************************ 
  2. 用c实现HASH表创建、插入、查找、删除、打印,实现并不是最完美的,欢迎指正补充!后续还有更多类似的实现放上来, 
  3. 欢迎关注!!! 
  4. 对本贴有更好的方法或建议可以给本人留言或发邮件: 
  5. Email:zww0815@qq.com 
  6. Thanks! 
  7. ************************************************************************/  
  8. #include <stdio.h>  
  9. #include <STDLIB.H>  
  10. #include <MEMORY.H>  
  11.   
  12. #define STATUS int  
  13. #define FALSE 0  
  14. #define TRUE 1  
  15. #define VOID void  
  16.   
  17. /**************************************** 
  18. a)定义hash表和基本数据节点   
  19. *****************************************/  
  20. typedef struct _Node  
  21. {  
  22.     int data;  
  23.     struct _Node *next;  
  24. }NODE;  
  25.   
  26. typedef struct _HASH_TABLE  
  27. {  
  28.     NODE * value[10];  
  29. }HASH_TABLE;  
  30.   
  31.   
  32. /**************************************** 
  33. b)创建hash表 
  34. *****************************************/  
  35. HASH_TABLE * create_hash_table()  
  36. {  
  37.     HASH_TABLE* pHashTbl = (HASH_TABLE*)malloc(sizeof(HASH_TABLE));  
  38.     memset(pHashTbl,0,sizeof(HASH_TABLE));  
  39.     return pHashTbl;  
  40. }  
  41.   
  42.   
  43. /**************************************** 
  44. c)在hash表当中寻找数据 
  45. *****************************************/  
  46. NODE * find_data_in_hash(HASH_TABLE* pHashTbl,int data)  
  47. {  
  48.     NODE* pNode;  
  49.     if (NULL == pHashTbl)  
  50.     {  
  51.         return NULL;  
  52.     }  
  53.   
  54.     /*获得HASH表索引,为NULL则直接返回NULL*/  
  55.     if (NULL == (pNode = pHashTbl->value[data%10]))  
  56.     {  
  57.         return NULL;  
  58.     }  
  59.   
  60.     /*在该索引下的单链表中查找节点*/  
  61.     while(pNode)  
  62.     {  
  63.         if ( data == pNode->data)  
  64.         {  
  65.             /*找到节点就返回当前节点*/  
  66.             return pNode;  
  67.         }  
  68.         /*当前节点不是,指向下一节点*/  
  69.         pNode = pNode->next;  
  70.     }  
  71.   
  72.     /*没找到返回NULL,不过在这返回没有意义*/  
  73.     //return NULL;  
  74. }  
  75.   
  76. /**************************************** 
  77. d)在hash表当中插入数据 
  78. *****************************************/  
  79. STATUS insert_data_into_hash(HASH_TABLE* pHashTbl,int data)  
  80. {  
  81.     NODE* pNode;  
  82.     if (NULL == pHashTbl)  
  83.     {  
  84.         return FALSE;  
  85.     }  
  86.   
  87.     if (NULL == pHashTbl->value[data%10])  
  88.     {  
  89.         pNode = (NODE*)malloc(sizeof(NODE));  
  90.         memset(pNode,0,sizeof(NODE));  
  91.         pNode->data = data;  
  92.         pHashTbl->value[data%10] = pNode;  
  93.   
  94.         return TRUE;  
  95.     }  
  96.   
  97.     if (NULL == find_data_in_hash(pHashTbl,data))  
  98.     {  
  99.         return FALSE;  
  100.     }  
  101.   
  102.     pNode = pHashTbl->value[data%10];  
  103.     while(pNode->next)  
  104.     {  
  105.         pNode = pNode->next;  
  106.     }  
  107.   
  108.     pNode->next = (NODE*)malloc(sizeof(NODE));  
  109.     memset(pNode->next,0,sizeof(NODE));  
  110.     pNode->next->data = data;  
  111.   
  112.     return TRUE;  
  113. }  
  114.   
  115. /**************************************** 
  116.  e)从hash表中删除数据 
  117. *****************************************/  
  118. STATUS delete_data_from_hash(HASH_TABLE* pHashTbl,int data)  
  119. {  
  120.     NODE* pHead;  
  121.     NODE* pNode;  
  122.   
  123.     if (NULL == pHashTbl || NULL == pHashTbl->value[data%10])  
  124.     {  
  125.         return FALSE;  
  126.     }  
  127.   
  128.     if (NULL == (pNode = find_data_in_hash(pHashTbl,data)))  
  129.     {  
  130.         return FALSE;  
  131.     }  
  132.   
  133.     if (pNode == pHashTbl->value[data%10])  
  134.     {  
  135.         pHashTbl->value[data%10] = pNode->next;  
  136.         goto final;  
  137.     }  
  138.   
  139.     pHead = pHashTbl->value[data%10];  
  140.       
  141.     while(pNode != pHead->next)  
  142.     {  
  143.         pHead = pHead->next;  
  144.     }  
  145.   
  146.     pHead->next = pNode->next;  
  147.   
  148. final:  
  149.     free(pNode);  
  150.     return TRUE;  
  151. }  
  152.   
  153.   
  154. /**************************************** 
  155.  f)打印hash表中所有数据 
  156.  如: 
  157.  [Hash idx]     [value] 
  158.  0-------------NULL 
  159.  1-------------1 251 
  160.  2-------------22 
  161.  3-------------123 43 
  162.  4-------------NULL 
  163.  5-------------55 15 235 525 725 275 545 
  164.  6-------------NULL 
  165.  7-------------257 
  166.  8-------------NULL 
  167. *****************************************/  
  168. VOID print_hash_data(HASH_TABLE* pHashTbl)  
  169. {  
  170.     NODE* pNode;  
  171.     int i=0;  
  172.     if (NULL == pHashTbl)  
  173.     {  
  174.         printf("ERROR:The hash is NULL\n");  
  175.     }  
  176.   
  177. /* 
  178.     if (NULL == (pNode = pHashTbl->value[10])) 
  179.     { 
  180.         printf("ERROR:The hash node is NULL\n"); 
  181.     } 
  182. */  
  183.     printf("[Hash idx]     [value]\n");  
  184.     do   
  185.     {     
  186.         printf("   %d-------------",i);  
  187.         if (NULL == pHashTbl->value[i])  
  188.         {  
  189.             i++;  
  190.             printf("NULL\n");  
  191.             continue;  
  192.         }  
  193.   
  194.         pNode = pHashTbl->value[i];  
  195.           
  196.         while(pNode)  
  197.         {  
  198.             printf("%d ",pNode->data);  
  199.             pNode = pNode->next;  
  200.         }  
  201.         printf("\n");  
  202.         i++;  
  203.     } while (i<10);  
  204.   
  205.     printf("\n");  
  206. }  
  207.   
  208. int main()  
  209. {  
  210.     HASH_TABLE* pHashTbl = create_hash_table();  
  211.       
  212.     (VOID)insert_data_into_hash(pHashTbl,22);  
  213.     (VOID)insert_data_into_hash(pHashTbl,22);  
  214.     (VOID)insert_data_into_hash(pHashTbl,123);  
  215.     (VOID)insert_data_into_hash(pHashTbl,436);  
  216.     (VOID)insert_data_into_hash(pHashTbl,55);  
  217.     (VOID)insert_data_into_hash(pHashTbl,157);  
  218.     (VOID)insert_data_into_hash(pHashTbl,235);  
  219.     (VOID)insert_data_into_hash(pHashTbl,256);  
  220.     (VOID)insert_data_into_hash(pHashTbl,525);  
  221.     (VOID)insert_data_into_hash(pHashTbl,724);  
  222.     (VOID)insert_data_into_hash(pHashTbl,278);  
  223.     (VOID)insert_data_into_hash(pHashTbl,209);  
  224.     (VOID)insert_data_into_hash(pHashTbl,67);  
  225.     (VOID)insert_data_into_hash(pHashTbl,54);  
  226.     (VOID)insert_data_into_hash(pHashTbl,546);  
  227.     (VOID)insert_data_into_hash(pHashTbl,350);  
  228.     (VOID)insert_data_into_hash(pHashTbl,101);  
  229.     (VOID)insert_data_into_hash(pHashTbl,23);  
  230.   
  231.     print_hash_data(pHashTbl);  
  232.   
  233.     (VOID)delete_data_from_hash(pHashTbl,55);  
  234.   
  235.     print_hash_data(pHashTbl);  
  236.   
  237.   
  238.     return 0;  
  239. }  
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值