hash 算法-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 753010
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1150
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-17 14:32
个人简介

小公司研发总监,既当司令也当兵!

文章分类

全部博文(144)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: linux

2016-01-02 22:40:29

hash算法原理本文就不累赘讲述了,百度/google一下就知道了。

本demo程序实现一个简单的联系人hash表,通过姓名查找联系人:
1. hash函数采用简单的姓名字母累加和,通过掩码映射的方式获取hash-key;
2. 冲突解决采用链式散列;
3.由于联系人姓名(拼音)累加和分布相对集中(几百到两千左右),因此该hash表容量一般小于2000冲突较少,如果联系人数量较多,该hash函数冲突会增加。

点击(此处)折叠或打开

  1. /**
  2. * 联系人hash表实现demo
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>

  7. struct hashtableentry {
  8.     int flag;
  9.     int nextindex;
  10.     int prveindex;
  11.     char* name;
  12. };
  13.     
  14. struct peoplehashtable {
  15.     unsigned int size;
  16.     unsigned int entrys;
  17.     struct hashtableentry *datas;
  18. };

  19. enum entryflag {
  20.     state_unused = 0,
  21.     state_hash_used,
  22.     state_collision_used
  23. };

  24. int getuppersize(int size)
  25. {
  26.     int uppersize = 1;

  27.     if (size <= 0)
  28.         return 1;

  29.     while (uppersize < size)
  30.         uppersize <<= 1;

  31.     return uppersize;

  32. }

  33. int gethashkey(struct peoplehashtable *table, const char *name)
  34. {
  35.     char *ptr;
  36.     unsigned key = 0;

  37.     if (null == name)
  38.         return -1;

  39.     ptr = name;

  40.     while ('\0' != *ptr)
  41.     {
  42.         key = *ptr;
  43.         ptr;
  44.     }

  45.     printf("sum of %s: [%d], and key : [%d]\n",name, key, key & (table->size -1));
  46.     if (null == table)
  47.         return key;

  48.     return (key & (table->size -1));
  49. }

  50. int insert2table(struct peoplehashtable *table, char *name)
  51. {
  52.     int key = 0;
  53.     struct hashtableentry *entry;
  54.     struct hashtableentry *tmp;

  55.     int i;

  56.     if (null == table)
  57.         return -1;

  58.     if (table->entrys == table->size)
  59.     {
  60.         printf("table is full, can not insert only more!\n");
  61.         return -1;
  62.     }

  63.     key = gethashkey(table, name);
  64.     entry = &(table->datas[key]);

  65.     if (entry->flag == state_unused)
  66.     {
  67.         entry->flag = state_hash_used;
  68.         entry->name = name;
  69.         table->entrys;
  70.         return 0;
  71.     }

  72.     if (entry->flag == state_hash_used)
  73.     {
  74.         // find a useable entry
  75.         for (i = 0; i < table->size; i)
  76.         {
  77.             if (table->datas[i].flag == state_unused)
  78.                 break;
  79.         }

  80.         // find if entry has a collision entry
  81.         while( entry->nextindex != -1)
  82.         {
  83.             entry = &table->datas[entry->nextindex];
  84.         }

  85.         tmp = &(table->datas[i]);

  86.         tmp->flag = state_collision_used;
  87.         tmp->name = name;
  88.         tmp->prveindex = key;

  89.         entry->nextindex = i;
  90.         table->entrys;
  91.         return 0;
  92.     }
  93.     
  94.     if (entry->flag == state_collision_used)
  95.     {
  96.         // find a useable entry
  97.         for (i = 0; i < table->size; i)
  98.         {
  99.             if (table->datas[i].flag == state_unused)
  100.                 break;
  101.         }

  102.         // remove current entry to the empty entry space
  103.         tmp = &table->datas[entry->prveindex];
  104.         tmp->nextindex = i;

  105.         tmp = &table->datas[i];
  106.         tmp->prveindex = entry->prveindex;
  107.         tmp->flag = entry->flag;
  108.         tmp->name = entry->name;
  109.         tmp->nextindex = entry->nextindex;

  110.         entry->flag = state_hash_used;
  111.         entry->name = name;
  112.         entry->prveindex = -1;
  113.         entry->nextindex = -1;

  114.         table->entrys;
  115.         return 0;
  116.     }

  117.     return -1;
  118. }

  119. struct hashtableentry* findpeople(struct peoplehashtable *table, char *name)
  120. {
  121.     int key;
  122.     struct hashtableentry *entry = null;

  123.     if (null == table)
  124.         return null;

  125.     key = gethashkey(table, name);

  126.     entry = &table->datas[key];
  127.     if (entry->name != null && !strcmp(entry->name, name))
  128.     {
  129.         return entry;
  130.     }

  131.     while(entry)
  132.     {
  133.         if (entry->name && !strcmp(entry->name, name))
  134.             return entry;

  135.         if (entry->nextindex != -1)
  136.             entry = &table->datas[entry->nextindex];
  137.         else
  138.             return null;
  139.     }
  140.     return null;
  141. }

  142. int createhashtable(struct peoplehashtable **table, unsigned int size)
  143. {
  144.     int uppersize = getuppersize(size);

  145.     struct peoplehashtable *tableptr = null;
  146.     struct hashtableentry *entrylist = null;
  147.     int i;

  148.     tableptr = (struct peoplehashtable*)malloc(sizeof(*tableptr));
  149.     if (null == tableptr)
  150.         return -1;

  151.     entrylist = (struct hashtableentry*)malloc(sizeof(*entrylist) * uppersize);
  152.     if (null == entrylist)
  153.     {
  154.         free(tableptr);
  155.         return -1;
  156.     }
  157.     memset(entrylist, 0, sizeof(*entrylist) * uppersize);
  158.     for(i = 0; i < uppersize; i)
  159.     {
  160.         entrylist[i].flag = state_unused;
  161.         entrylist[i].name = null;
  162.         entrylist[i].nextindex = -1;
  163.         entrylist[i].prveindex = -1;
  164.     }

  165.     tableptr->datas = entrylist;
  166.     tableptr->entrys = 0;
  167.     tableptr->size = uppersize;

  168.     *table = tableptr;

  169.     return 0;
  170. }

  171. int destoryhashtable(struct peoplehashtable *table)
  172. {
  173.     if (null == table)
  174.         return 0;

  175.     free(table->datas);
  176.     free(table);

  177.     return 0;
  178. }

  179. static void debug_print_table(struct peoplehashtable *table)
  180. {
  181.     int i;

  182.     printf("table infos:\n");
  183.     printf("size: [%d], entry numbers;[%d]\n", table->size,table->entrys);
  184.     printf("index flag prve next name\n");
  185.     for (i = 0; i < table->size; i)
  186.     {
  187.         printf("]]]] %s\n", i,table->datas[i].flag ,table->datas[i].prveindex, table->datas[i].nextindex, table->datas[i].name == null ? "--" :table->datas[i].name);
  188.     }
  189. }
  190. /*
  191. void main()
  192. {
  193.     char *peoples[30]= {
  194.         "刘敏",
  195.         "wang wu",
  196.         "li si",
  197.         "liu yu gu",
  198.         "jiang jie hui",
  199.         "tan wei wei",
  200.         "liu tao",
  201.         "mi yue",
  202.         "mi shu",
  203.         "yin si",
  204.         "yin dang",
  205.         "huang zi",
  206.         "zhang zi",
  207.         "qu yuan",
  208.         "tang jia jun",
  209.         "gui wa zi",
  210.         "qu li zi",
  211.         "gong zhu",
  212.         "tang hui",
  213.         "jiu zhong",
  214.         ""
  215.     };

  216.     struct peoplehashtable *table;
  217.     struct hashtableentry *entry;

  218.     int ret;
  219.     int i;

  220.     ret = createhashtable(&table, 20);

  221.     if (ret < 0)
  222.     {
  223.         printf("create hash table fail\n");
  224.         return;
  225.     }

  226.     i = 0;
  227.     for (i = 0; i < 20; i)
  228.     {
  229.         insert2table(table,peoples[i]);
  230.     }
  231.     printf("-- add people done!\n");
  232.     
  233.     debug_print_table(table);


  234.     entry = findpeople(table, "jiu zhong");
  235.     if (entry != null)
  236.     {
  237.         printf("find!\n");
  238.     }
  239.     else
  240.         printf("--no--\n");

  241.     destoryhashtable(table);
  242.     table = null;
  243. }*/



阅读(2311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图