哈希表的C实现(一)
2013-12-08 23:34
246 查看
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。具体的介绍网上有很详细的描述,如闲聊哈希表 ,这里就不再累述了;
哈希表在像Java、C#等语言中是与生俱来的。可是在C的世界中,似乎只有自己动手,丰衣足食;在网上google了一把,大致有几个版本,我会一一来分析对比;
首先先来交代一下哈希表实现中需要注意的一些概念:
(主要参考:这里)
哈希函数
也叫散列函数,即:根据key,计算出key对应记录的储存位置
position = f(key)
散列函数满足以下的条件:
1、对输入值运算,得到一个固定长度的摘要(Hash value);
2、不同的输入值可能对应同样的输出值;
以下的函数都可以认为是一个散列函数:
f(x) = x mod 16; (1)
f(x) = (x2 + 10) * x; (2)
f(x) = (x | 0×0000FFFF) XOR (x >> 16); (3)
不过,仅仅满足上面这两条的函数,作为散列函数,还有不足的地方。我们还希望散列函数满足下面几点:
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, 开放地址法
下面就来看看每种方法的具体实现吧:
链地址法:
举例说明: 设有 8 个元素 { a,b,c,d,e,f,g,h } ,采用某种哈希函数得到的地址分别为: {0 , 2 , 4 , 1 , 0 , 8 , 7 , 2} ,当哈希表长度为 10 时,采用链地址法解决冲突的哈希表如下图所示。
图片及举例引自:这里
哈希表在像Java、C#等语言中是与生俱来的。可是在C的世界中,似乎只有自己动手,丰衣足食;在网上google了一把,大致有几个版本,我会一一来分析对比;
首先先来交代一下哈希表实现中需要注意的一些概念:
(主要参考:这里)
哈希函数
也叫散列函数,即:根据key,计算出key对应记录的储存位置
position = f(key)
散列函数满足以下的条件:
1、对输入值运算,得到一个固定长度的摘要(Hash value);
2、不同的输入值可能对应同样的输出值;
以下的函数都可以认为是一个散列函数:
f(x) = x mod 16; (1)
f(x) = (x2 + 10) * x; (2)
f(x) = (x | 0×0000FFFF) XOR (x >> 16); (3)
不过,仅仅满足上面这两条的函数,作为散列函数,还有不足的地方。我们还希望散列函数满足下面几点:
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, 开放地址法
下面就来看看每种方法的具体实现吧:
链地址法:
举例说明: 设有 8 个元素 { a,b,c,d,e,f,g,h } ,采用某种哈希函数得到的地址分别为: {0 , 2 , 4 , 1 , 0 , 8 , 7 , 2} ,当哈希表长度为 10 时,采用链地址法解决冲突的哈希表如下图所示。
图片及举例引自:这里
#include "stdafx.h" #include <string.h> #include <stdio.h> #include <stdlib.h> typedef struct _node{ char *name; char *desc; struct _node *next; }node; #define HASHSIZE 101 static node* hashtab[HASHSIZE]; void inithashtab(){ int i; for(i=0;i<HASHSIZE;i++) hashtab[i]=NULL; } unsigned int hash(char *s){ unsigned int h=0; for(;*s;s++) h=*s+h*31; return h%HASHSIZE; } node* lookup(char *n){ unsigned int hi=hash(n); node* np=hashtab[hi]; for(;np!=NULL;np=np->next){ if(!strcmp(np->name,n)) return np; } return NULL; } char* m_strdup(char *o){ int l=strlen(o)+1; char *ns=(char*)malloc(l*sizeof(char)); strcpy(ns,o); if(ns==NULL) return NULL; else return ns; } char* get(char* name){ node* n=lookup(name); if(n==NULL) return NULL; else return n->desc; } int install(char* name,char* desc){ unsigned int hi; node* np; if((np=lookup(name))==NULL){ hi=hash(name); np=(node*)malloc(sizeof(node)); if(np==NULL) return 0; np->name=m_strdup(name); if(np->name==NULL) return 0; np->next=hashtab[hi]; hashtab[hi]=np; } else free(np->desc); np->desc=m_strdup(desc); if(np->desc==NULL) return 0; return 1; } /* A pretty useless but good debugging function, which simply displays the hashtable in (key.value) pairs */ void displaytable(){ int i; node *t; for(i=0;i<HASHSIZE;i++){ if(hashtab[i]==NULL) printf("()"); else{ t=hashtab[i]; printf("("); for(;t!=NULL;t=t->next) printf("(%s.%s) ",t->name,t->desc); printf(".)"); } } } void cleanup(){ int i; node *np,*t; for(i=0;i<HASHSIZE;i++){ if(hashtab[i]!=NULL){ np=hashtab[i]; while(np!=NULL){ t=np->next; free(np->name); free(np->desc); free(np); np=t; } } } } main(){ int i; char* names[]={"name","address","phone","k101","k110"}; char* descs[]={"Sourav","Sinagor","26300788","Value1","Value2"}; inithashtab(); for(i=0;i<5;i++) install(names[i],descs[i]); printf("Done"); printf("If we didnt do anything wrong..""we should see %s",get("k110")); install("phone","9433120451"); printf("Again if we go right, we have %s and %s",get("k101"),get("phone")); /*displaytable();*/ cleanup(); return 0; }
相关文章推荐
- 点击其它地方关闭DIV
- Jeecg Tag的扩展参数,及其虚拟布局
- 异常11
- 菜鸟之学习51单片机(五)蜂鸣器继电器的实现
- POJ 3176 Cow Bowling (简单DP)
- 内部类与匿名内部类
- JAVA规则引擎 -- Drools
- 打印螺旋数阵的一种实现
- Windows Phone 7 开发环境的搭建
- C# POST数据到指定页面,并跳转至该页面
- Object9
- uboot 移植
- Eclipse插件篇——Tomcat
- shell read
- memcached应用
- Spring声明式事务配置管理方法(查找总结)
- JAVA获取CLASSPATH路径
- Apache(Web)服务器性能调整
- 用java实现对文件夹的拷贝
- Java主线程等待子线程、线程池