超好用的uthash万岁~~~

  1. #include "uthash.h"  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <unistd.h>  
  5.   
  6.   
  7. /*这个uthash必须构造一个结构体*/  
  8. struct packet  
  9. {  
  10.         int key;       /*这个是用来做hash的key值*/  
  11.         char msg[10];   
  12.         UT_hash_handle hh; /*这个结构是uthash的结构体,里面包含next,prev,hash值等信息*/  
  13. };  
  14.   
  15. int main()  
  16. {  
  17.         struct packet *pkt, *tmp;  
  18.         int i;  
  19.   
  20.         struct packet *hash_packet = NULL; /*必须初始化为NULL*/  
  21.           
  22.   
  23.   
  24.         /*打印这个hash的节点数*/  
  25.         printf ("hash count = %d \n", HASH_COUNT(hash_packet));  
  26.   
  27.   
  28.   
  29.   
  30.         /*往hash中添加节点*/  
  31.   
  32.         for (i=0; i<10; i++)  
  33.         {  
  34.                 pkt = (struct packet *)malloc(sizeof(struct packet));  
  35.                 pkt->key=i;  
  36.                 sprintf (pkt->msg, "i=%d", i);  
  37.   
  38.                 HASH_FIND_INT(hash_packet, &i, tmp);  
  39.                 if (tmp != NULL)  
  40.                 {  
  41.                         printf ("The key(%d) exists in hash. \n", i);  
  42.                         continue;  
  43.                 }  
  44.                 HASH_ADD_INT(hash_packet, key, pkt);  
  45.                 printf ("insert item. key=%d,value=%p \n", i, pkt);  
  46.         }  
  47.         printf ("hash count = %d \n", HASH_COUNT(hash_packet));  
  48.   
  49.   
  50.   
  51.   
  52.         /*通过key查找*/  
  53.         for (i=0; i<13; i++)  
  54.         {  
  55.                 HASH_FIND_INT(hash_packet, &i, tmp);  
  56.                 if (tmp == NULL)  
  57.                 {  
  58.                         printf ("find not item. key=%d,value=%p \n", i, tmp);  
  59.                         continue;  
  60.                 }  
  61.                 printf ("find item. key=%d,value=%p \n", i, tmp);  
  62.         }  
  63.   
  64.         printf ("hash count = %d \n", HASH_COUNT(hash_packet));  
  65.   
  66.   
  67.   
  68.   
  69.         /*遍历这个hash表*/  
  70.         for (tmp=hash_packet; tmp != NULL; tmp=tmp->hh.next)  
  71.                 printf (" %d => %s \n", tmp->key, tmp->msg);  
  72.         /*删除节点*/  
  73.         for (i=0; i<13; i++)  
  74.         {  
  75.                 HASH_FIND_INT(hash_packet, &i, tmp);  
  76.                 if (tmp == NULL)  
  77.                 {  
  78.                         printf ("find not item. key=%d,value=%p \n", i, tmp);  
  79.                         continue;  
  80.                 }  
  81.   
  82.                 /*删除节点不会释放你的空间必须自己释放*/  
  83.                 HASH_DEL(hash_packet, tmp);  
  84.                 free(tmp);  
  85.                 printf ("delete itme. key=%d,value=%p \n", i, tmp);  
  86.         }  
  87.         printf ("hash count = %d \n", HASH_COUNT(hash_packet));  
  88.         return 0;  
  89. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值