拉链法实现哈希表
来源:互联网 发布:谷歌输入法 mac 编辑:程序博客网 时间:2024/05/16 01:26
/**哈希表 拉链法*/#include<stdio.h>#include<stdlib.h>#define MinTableSize 10typedef int ElemType;typedef unsigned int Index;typedef struct ListNode{ElemType element;structListNode *next;}*Position;typedef Position List;/* List *TheList will be an array of lists, allocated later *//* The lists use headers (for simplicity), *//* though this wastes space */typedef struct HashTbl{int TableSize; List *TheLists;}*HashTable;int NextPrime(int N){int i;if(N%2==0)N++;for(;;N+=2){for(i=3;i*i<=N;i+=2)if(N%i==0)return 0;return N;}}/*Hash function for ints*/Index Hash(ElemType Key,int TableSize){return Key%TableSize;}HashTable InitializeTable(int TableSize){HashTable H;int i;if(TableSize<MinTableSize){printf("Table size too small!\n");return NULL;}/*Allocate table*/H=(HashTable)malloc(sizeof(struct HashTbl));if(NULL==H) printf("Out of space!!!\n");H->TableSize=NextPrime(TableSize);/*Allocate array of lists*/H->TheLists=(List *)malloc(sizeof(List)*H->TableSize);if(NULL==H->TheLists){ printf("Out of space!!!\n"); free(H); return NULL;}/*Allocate list headers*/for(i=0;i<H->TableSize;i++){H->TheLists[i]=(Position)malloc(sizeof(struct ListNode));if(NULL==H->TheLists[i])printf("Out of space!!!\n");elseH->TheLists[i]->next=NULL;H->TheLists[i]->element=0;//哈希表中所有元素的key初始化为0}return H;}Position Find(ElemType Key,HashTable H){Position p;List L;L=H->TheLists[Hash(Key,H->TableSize)];p=L->next;while(p!=NULL&&p->element!=Key)/*Probably need strcmp!!*/p=p->next;return p;}void Insert(ElemType Key,HashTable H){Position pos,newCell;List L;pos=Find(Key,H);if(NULL==pos)/*Key is not found*/{newCell=(Position)malloc(sizeof(struct ListNode));if(NULL==newCell) printf("Out of space!!!");else{L=H->TheLists[Hash(Key,H->TableSize)];newCell->next=L->next;newCell->element=Key;/*Probably need strcpy*/L->next=newCell;}}}void DestroyTable(HashTable H){int i;for(i=0;i<H->TableSize;i++){Position p=H->TheLists[i];Position temp;while(p!=NULL){temp=p->next;free(p);p=temp;}}free(H->TheLists);free(H);}void printHash(HashTable H,int len){int i;for(i=0;i<len;i++){Position p=H->TheLists[i];while(p){printf("address=%d value=%d\n",i,p->element);p=p->next;}}}int main(){HashTable H;Position p=NULL;int array[]={19,14,23,01,68,20,84,27,55,11,10,79};int len=sizeof(array)/sizeof(array[0]);int i;ElemType k; H=InitializeTable(len);for(i=0;i<len;i++){Insert(array[i],H);}printHash(H,len);printf("\n\n");printf("please input the value which need find:");scanf("%d",&k);p=Find(k,H);if(p) printf("%d",p->element);else printf("cannot find the value!");printf("\n\n"); printf("free the table\n");DestroyTable(H);printf("it's done!!!");printf("\n\n");return 0;}
- 拉链法实现哈希表
- 一个哈希表的简单实现拉链法
- 哈希表 拉链法
- 哈希表之拉链法
- 哈希表查找--拉链法
- 深入哈希表(三)--拉链法(哈希桶)实现哈希表
- HashTable哈希表拉链法实现(C语言)
- Java 实现的拉链法实现
- 哈希表查找 — 拉链法
- Pyhton Map 拉链法 的实现
- HashTable的拉链法的数组实现
- 拉链法实现字符串哈希
- 哈希桶的实现(拉链法)
- HASH表的java实现(拉链法)
- 拉链法
- 哈希表的【构造方法】【冲突处理方法】及【哈希拉链法的简单代码实现】
- 哈希表(闭散列、拉链法--哈希桶)
- 哈希表(闭散列、拉链法--哈希桶)
- ACMSTEP 2.3.5 How Many Trees? //高精度卡特兰数
- 项目管理之敏捷与瀑布
- Poj3974 最长回文子串
- 修改的一个导出DataSet到xls的单元
- [转载]槽函数教程
- 拉链法实现哈希表
- HDUOJ ACMSTEP 章节题型分类 持续更新中(9.14修正)
- 心声论坛笑话之二:啊~我没电了
- UVA 972 Horizon Line
- 地址连接
- nginx 根据不同URL分发到不同服务器
- c#跳转的几种语句
- POJ 1031 Fence
- 安装JDK遇到的问题