数据结构——哈希表使用

打印 上一主题 下一主题

主题 1026|帖子 1026|积分 3078

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
        目的:使用哈希表存放若干个单词,用户输入某个单词,查询在哈希表中是否存在该单词
主函数 main.c ↓↓↓↓↓
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "hashtable.h"
  5. int main(void)
  6. {
  7.         struct list_head *parray = NULL;
  8.         int i = 0;
  9.         char str[5][32] = {0};
  10.         char num[32] = {0};
  11.         char cnt[32] = {0};
  12.         for(i = 0; i < 5; i++)
  13.         {
  14.                 gets(str[i]);
  15.         }
  16.        
  17.         parray = create_hashtable();
  18.        
  19.         for(i = 0; i < 5; i++)
  20.         {
  21.                 insert_hashtable(parray, str[i]);
  22.         }
  23.         show_hashtable(parray);
  24.         printf("请输入要查询的单词:\n");
  25.         gets(num);
  26. #if 1
  27.         if (find_hashtable(parray, num))
  28.         {
  29.                 printf("%s 单词存在\n", num);
  30.         }
  31.         else
  32.         {
  33.                 printf("%s 节点不存在\n", num);
  34.         }
  35. #endif
  36.         printf("请输入要删除的单词:\n");
  37.         gets(num);
  38.         delete_hashtable(parray, num);
  39.         printf("已删除 %s 此单词\n", num);
  40.         show_hashtable(parray);
  41.         destroy_hashtable(parray);
  42.         parray = NULL;
  43.         return 0;
  44. }
复制代码
封装函数 ↓↓↓↓↓
  1. #include "hashtable.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. //创建
  6. struct list_head *create_hashtable(void)
  7. {   
  8.     int i = 0;
  9.     struct list_head *ptmd = NULL;
  10.     ptmd = malloc(MAXNUM * sizeof(struct list_head));
  11.     if(NULL == ptmd)
  12.     {
  13.         return NULL;
  14.     }
  15.     for(i = 0; i < MAXNUM; i++)
  16.     {
  17.         INIT_LIST_HEAD(&ptmd[i]);
  18.     }
  19.     return ptmd;
  20. }
  21. //判断
  22. static int asc_compare(struct list_head *pnew, struct list_head *pnode)
  23. {
  24.     return strcmp((list_entry(pnew, hashnode_t, node)->str), (list_entry(pnode, hashnode_t, node)->str));
  25. }
  26. //插入
  27. int insert_hashtable(struct list_head *pheadlist, char *pstr)
  28. {
  29.     int key = 0;
  30.     hashnode_t *ptmd = NULL;
  31.     ptmd = malloc(sizeof(hashnode_t));
  32.     if(NULL == ptmd)
  33.     {
  34.         return -1;
  35.     }
  36.     strcpy(ptmd->str, pstr);
  37.     if(pstr[1] >= 'A' && pstr[1] <= 'Z')
  38.     {
  39.         key = *pstr - 'A';
  40.     }
  41.     else if(pstr[1] >= 'a' && pstr[1] <= 'z')
  42.     {
  43.         key = *pstr - 'a' + 26;
  44.     }
  45.     list_add_order(&ptmd->node, &pheadlist[key], asc_compare);
  46.     return 0;
  47. }
  48. //打印
  49. int show_hashtable(struct list_head *pheadlist)
  50. {
  51.     int i = 0;
  52.     struct list_head *ptmpnode = NULL;
  53.     for (i = 0; i < MAXNUM; i++)
  54.     {
  55.         if(i >= 0 && i < 26)
  56.         {
  57.             printf("%c: ", (char)i + 'A');//或者+65
  58.         }
  59.         else
  60.         {
  61.             printf("%c: ", (char)i + 'a' - 26);//或者+71
  62.         }
  63.         list_for_each(ptmpnode, &pheadlist[i])
  64.         {
  65.             printf(" %s----", list_entry(ptmpnode, hashnode_t, node)->str);
  66.         }
  67.         printf("\n");
  68.     }
  69.     return 0;
  70. }
  71. //查找
  72. hashnode_t *find_hashtable(struct list_head *pheadlist, char *pstr)
  73. {
  74.     int key = 0;
  75.     struct list_head *ptmpnode = NULL;
  76.     if(pstr[1] >= 'A' && pstr[1] <= 'Z')
  77.     {
  78.         key = *pstr - 'A';
  79.     }
  80.     else if(pstr[1] >= 'a' && pstr[1] <= 'z')
  81.     {
  82.         key = *pstr - 'a' + 26;
  83.     }
  84.    
  85.     list_for_each(ptmpnode, &pheadlist[key])
  86.     {
  87.         if (strcmp(list_entry(ptmpnode, hashnode_t, node)->str, pstr) == 0)
  88.         {
  89.             return list_entry(ptmpnode, hashnode_t, node);
  90.         }
  91.         else if (list_entry(ptmpnode, hashnode_t, node)->str > pstr)
  92.         {
  93.             break;
  94.         }
  95.     }
  96.    
  97.     return NULL;
  98. }
  99. //删除
  100. int delete_hashtable(struct list_head *parray, char *pstr)
  101. {
  102.     hashnode_t *ptmpnode = NULL;
  103.     int cnt = 0;
  104.     while (1)
  105.     {
  106.         ptmpnode = find_hashtable(parray, pstr);
  107.         if (NULL == ptmpnode)
  108.         {
  109.             break;
  110.         }
  111.         list_del(&ptmpnode->node);
  112.         free(ptmpnode);
  113.         cnt++;
  114.     }
  115.    
  116.     return cnt;
  117. }
  118. //销毁
  119. int destroy_hashtable(struct list_head *parray)
  120. {
  121.     int i = 0;
  122.     hashnode_t *ptmpnode = NULL;
  123.     hashnode_t *pnextnode = NULL;
  124.     for (i = 0; i < MAXNUM; i++)
  125.     {
  126.         list_for_each_entry_safe(ptmpnode, pnextnode, &parray[i], node)
  127.         {
  128.             free(ptmpnode);
  129.         }
  130.     }
  131.     free(parray);
  132.     return 0;
  133. }
复制代码
结构体函数↓↓↓↓↓
  1. #ifndef __HASHTABLE_H__
  2. #define __HASHTABLE_H__
  3. #include "list.h"
  4. typedef struct hashnode
  5. {
  6.     struct list_head node;
  7.     char str[32];
  8. }hashnode_t;
  9. #define MAXNUM      52
  10. extern struct list_head *create_hashtable(void);
  11. extern int insert_hashtable(struct list_head *pheadlist, char *pstr);
  12. extern int show_hashtable(struct list_head *pheadlist);
  13. extern hashnode_t *find_hashtable(struct list_head *pheadlist, char *pstr);
  14. extern int delete_hashtable(struct list_head *parray, char *pstr);
  15. extern int destroy_hashtable(struct list_head *parray);
  16. #endif
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表