C 数据结构与算法 散列表
2016-04-07 00:00
393 查看
摘要: 哈希表
看到了关于 散列表 的一篇质量较高的博客(就不用自己写了,偷个懒)
http://www.nowamagic.net/academy/detail/3008010
这是一个系列的教程全部看完就差不多 有个概念了 但是还是不敢说 全懂
资质比较愚钝,木办法
下面的是实验代码 个人敲了一遍 理解了一些
看到了关于 散列表 的一篇质量较高的博客(就不用自己写了,偷个懒)
http://www.nowamagic.net/academy/detail/3008010
这是一个系列的教程全部看完就差不多 有个概念了 但是还是不敢说 全懂
资质比较愚钝,木办法
下面的是实验代码 个人敲了一遍 理解了一些
//#include "stdafx.h" #include "string.h" #include "stdio.h" #include "stdlib.h" //data struct typedef struct _node { char *name; //key char *desc; //value struct _node *next; //next node address } node; #define HASHSIZE 101 //define hashzize 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; } //look for the specific value 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","nickname"}; char* descs[]= {"Sourav","Sinagor","26300788","Value1","Value2","ningjin"}; inithashtab(); for(i=0; i < 6; i++) install(names[i],descs[i]); printf("Done"); printf("If we didnt do anything wrong..""we should see %s\n",get("k110")); install("phone","9433120451"); printf("Again if we go right, we have %s and %s",get("k101"),get("phone")); printf("\nnickname:%s\n", get("nickname")); /*displaytable();*/ cleanup(); return 0; }
相关文章推荐
- 为PHP7添加新的数据结构支持
- 《常见算法和数据结构》元素排序(4)——凸包问题
- 二叉树的创建即遍历(递归方式)
- 《常见算法和数据结构》元素排序(3)——洗牌算法
- 《常见算法和数据结构》元素排序(2)——希尔排序(动画)
- 树的前序遍历非递归、中序遍历非递归、后序遍历非递归
- 模拟实现操作系统内存分配与回收
- PHP学习(十一)--数组与数据结构
- 模拟实现操作系统调度算法
- 《常见算法和数据结构》元素排序(1)——简单排序(附动画)
- 数据结构:队列- 顺序队列
- Uva699 839二叉树的遍历
- 数据结构-----栈的实现
- 《数据结构》 定长顺序串常用操作代码集合
- 数据结构:栈
- 数据结构(左偏树,可并堆):BNUOJ 3943 Safe Travel
- c语言数据结构(3)
- 数据结构学习(一)
- mmap函数和mprotect函数
- Python 数据结构与算法 —— Prim 算法与小顶堆