(7)哈希表的链地址法实现


哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。哈希表在像Java、C#等语言中是与生俱来的。可是在C的世界中,似乎只有自己动手,丰衣足食了。

哈希表实现中需要注意的问题。

1. 哈希函数

也叫散列函数,即:根据key,计算出key对应记录的储存位置:position = f(key)

散列函数满足以下的条件:

  1. 对输入值运算,得到一个固定长度的摘要(Hash value);
  2. 不同的输入值可能对应同样的输出值;

以下的函数都可以认为是一个散列函数:

  • f(x) = x mod 16;
  • f(x) = (x2 + 10) * x; 
  • f(x) = (x | 0×0000FFFF) XOR (x >> 16); 

不过,仅仅满足上面这两条的函数,作为散列函数,还有不足的地方。我们还希望散列函数满足下面几点:

  1. 散列函数的输出值尽量接近均匀分布;
  2. x的微小变化可以使f(x)发生非常大的变化,即所谓“雪崩效应”(Avalanche effect);

上面两点用数学语言表示,就是:

  1. 输出值y的分布函数F(y)=y/m, m为散列函数的最大值。或记为y~U[0, m]
  2. |df(x)/dx| >> 1;
  • 从上面两点,大家看看,前面举例的三个散列函数,哪个更好呢?对了,是第三个:f(x) = (x | 0×0000FFFF) XOR (x >> 16);

它很完美地满足“好的散列函数”的两个附加条件。

2、哈希冲突(Hash collision)

也就是两个不同输入产生了相同输出值的情况。首先,哈希冲突是无法避免的,因此,哈希算法的选择直接决定了哈希冲突发送的概率;同时必须要对哈希冲突进行处理,方法主要有以下几种:

  1. 链地址法。即对Hash表中每个Hash值建立一个冲突表,即将冲突的几个记录以表的形式存储在其中。具体可以参照 散列冲突处理:链地址法 。
  2. 开放地址法。具体可以参照 散列冲突处理:开放定址法 。

  1. //#include "stdafx.h"  
  2. #include "string.h"  
  3. #include "stdio.h"  
  4. #include "stdlib.h"  
  5.   
  6. typedef struct _node  
  7. {  
  8.     char *name;  
  9.     char *desc;  
  10.     struct _node *next;  
  11. } node;  
  12.   
  13. #define HASHSIZE 101  
  14. static node* hashtab[HASHSIZE];  
  15.   
  16. void inithashtab()  
  17. {  
  18.     int i;  
  19.     for(i=0; i < HASHSIZE; i++)  
  20.         hashtab[i]=NULL;  
  21. }  
  22.   
  23. unsigned int hash(char *s)  
  24. {  
  25.     unsigned int h=0;  
  26.     for(; *s; s++)  
  27.         h=*s+h*31;  
  28.     return h%HASHSIZE;  
  29. }  
  30.   
  31. node* lookup(char *n)  
  32. {  
  33.     unsigned int hi=hash(n);  
  34.     node* np=hashtab[hi];  
  35.     for(; np!=NULL; np=np->next)  
  36.     {  
  37.         if(!strcmp(np->name,n))  
  38.             return np;  
  39.     }  
  40.   
  41.     return NULL;  
  42. }  
  43.   
  44. char* m_strdup(char *o)  
  45. {  
  46.     int l=strlen(o)+1;  
  47.     char *ns=(char*)malloc(l*sizeof(char));  
  48.     strcpy(ns,o);  
  49.     if(ns==NULL)  
  50.         return NULL;  
  51.     else  
  52.         return ns;  
  53. }  
  54.   
  55. char* get(char* name)  
  56. {  
  57.     node* n=lookup(name);  
  58.     if(n==NULL)  
  59.         return NULL;  
  60.     else  
  61.         return n->desc;  
  62. }  
  63.   
  64. int install(char* name,char* desc)  
  65. {  
  66.     unsigned int hi;  
  67.     node* np;  
  68.     if((np=lookup(name))==NULL)  
  69.     {  
  70.         hi=hash(name);  
  71.         np=(node*)malloc(sizeof(node));  
  72.         if(np==NULL)  
  73.             return 0;  
  74.         np->name=m_strdup(name);  
  75.         if(np->name==NULL) return 0;  
  76.         np->next=hashtab[hi];  
  77.         hashtab[hi]=np;  
  78.     }  
  79.     else  
  80.         free(np->desc);  
  81.     np->desc=m_strdup(desc);  
  82.     if(np->desc==NULL) return 0;  
  83.   
  84.     return 1;  
  85. }  
  86.   
  87. /* A pretty useless but good debugging function, 
  88. which simply displays the hashtable in (key.value) pairs 
  89. */  
  90. void displaytable()  
  91. {  
  92.     int i;  
  93.     node *t;  
  94.     for(i=0; i < HASHSIZE; i++)  
  95.     {  
  96.         if(hashtab[i]==NULL)  
  97.             printf("()");  
  98.         else  
  99.         {  
  100.             t=hashtab[i];  
  101.             printf("(");  
  102.             for(; t!=NULL; t=t->next)  
  103.                 printf("(%s.%s) ",t->name,t->desc);  
  104.             printf(".)");  
  105.         }  
  106.     }  
  107. }  
  108.   
  109. void cleanup()  
  110. {  
  111.     int i;  
  112.     node *np,*t;  
  113.     for(i=0; i < HASHSIZE; i++)  
  114.     {  
  115.         if(hashtab[i]!=NULL)  
  116.         {  
  117.             np=hashtab[i];  
  118.             while(np!=NULL)  
  119.             {  
  120.                 t=np->next;  
  121.                 free(np->name);  
  122.                 free(np->desc);  
  123.                 free(np);  
  124.                 np=t;  
  125.             }  
  126.         }  
  127.     }  
  128. }  
  129.   
  130. main()  
  131. {  
  132.     int i;  
  133.     char* names[]= {"name","address","phone","k101","k110"};  
  134.     char* descs[]= {"Sourav","Sinagor","26300788","Value1","Value2"};  
  135.   
  136.     inithashtab();  
  137.     for(i=0; i < 5; i++)  
  138.         install(names[i],descs[i]);  
  139.   
  140.     printf("Done");  
  141.     printf("If we didnt do anything wrong..""we should see %s\n",get("k110"));  
  142.   
  143.     install("phone","9433120451");  
  144.   
  145.     printf("Again if we go right, we have %s and %s",get("k101"),get("phone"));  
  146.   
  147.     /*displaytable();*/  
  148.     cleanup();  
  149.     return 0;  
  150. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include typedef struct node { int data; struct node *next; }node; init_hash(node **A,int n) { int i; for(i=0;idata=0; A[i]->next=NULL; } } insert_hash(node **A,int value,int n) { int key; node *p,*q; key=value%n; if(A[key]->next!=NULL) { p=A[key]->next; while(p->next!=NULL) p=p->next; q=(node *)malloc(sizeof(node)); q->data=value; q->next=NULL; p->next=q; } else { q=(node *)malloc(sizeof(node)); q->data=value; q->next=NULL; A[key]->next=q; } } int search_hash(node **A,int value,int n) { int key; node *p; key=value%n; if(A[key]->next==NULL) return 0; else { p=A[key]->next; while(p!=NULL) { if(p->data==value) return 1; } return 0; } } delete_hash(node **A,int value,int n) { int key; node *p,*q; key=value%n; p=A[key]; q=A[key]->next; while(q->data!=value) { p=q; q=q->next; } p->next=q->next; free(q); } print_hash(node **A,int n) { int i; node *p; for(i=0;inext!=NULL) { p=A[i]->next; while(p!=NULL) { printf("%d ",p->data); p=p->next; } } } printf("\n"); } main() { int i,n,value,Case; node **A; printf("输入待排序元素个数:\n"); scanf("%d",&n); A=(node **)malloc(sizeof(node*)*n); //申请一个指针型数组A[n] init_hash(A,n);//初始化数组A printf("输入hash表的值(空格键分开):\n"); for(i=0;i<n;i++) //建hash表 { scanf("%d",&value); insert_hash(A,value,n); } printf("请选择hash表的操作\n1.查询\t2.插入\t3.删除\t4.输出\n"); scanf("%d",&Case); switch(Case) { case 1: { printf("请输入要查询的元素:"); scanf("%d",&value); printf(search_hash(A,value,n)?"Success!\n":"Failure!\n"); //查询 } case 2: { printf("请输入要插入的元素:"); scanf("%d",&value); insert_hash(A,value,n); //插入 } case 3: { printf("请输入要删除的元素:"); scanf("%d",&value); printf(search_hash(A,value,n)?"删除成功!\n":"表中不存在该元素!\n"); delete_hash(A,value,n); } case 4: //输出 { printf("表中元素为:\n"); print_hash(A,n); } } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值