开放定址散列表--C语言实现
来源:互联网 发布:excel数据有效性在哪 编辑:程序博客网 时间:2024/05/21 14:55
hashtable.h
#ifndef _HASHTABLE_H_#define _HASHTABLE_H_#define MinTableSize 10000#define Prime 10007typedef unsigned int Index;typedef Index Position;struct HashTbl;typedef struct HashTbl* HashTable;typedef int ElementType;typedef struct HashEntry Cell;enum KindOfEntry {Legitimate ,Empty,Deleted};HashTable InitializeTable(int TableSize);void DestroyTable(HashTable H);Position Find(ElementType Key, HashTable H);void Insert(ElementType Key, HashTable H);ElementType Retrieve(Position P,HashTable H);HashTable Rehash(HashTable H);#endif
hashtable.c
#include<stdio.h>#include<stdlib.h>#include"hashtable.h"struct HashEntry{ ElementType Element; enum KindOfEntry Info;};struct HashTbl{ int TableSize; Cell *TheCells;};int Hash(ElementType Key, int TableSize){ return Key%TableSize;}HashTable InitializeTable(int TableSize){ HashTable H; int i; if (TableSize < MinTableSize) { printf("ERROR!\n"); return NULL; } H = (HashTable)malloc(sizeof(HashTbl)); if (H == NULL) { printf("FAILURE!\n"); exit(EXIT_FAILURE); } H->TableSize = Prime;//Prime随TableSize变化而变化 H->TheCells = (Cell *)malloc(sizeof(Cell)*H->TableSize); if (H->TheCells == NULL) { printf("FAILURE!\n"); exit(EXIT_FAILURE); } for (i = 0; i < H->TableSize; i++) H->TheCells[i].Info = Empty; return H;}void DestroyTable(HashTable H){ if (H) free(H->TheCells);}Position Find(ElementType Key, HashTable H){ Position CurrentPos; int CollisionNum; CollisionNum = 0; CurrentPos = Hash(Key, H->TableSize); while (H->TheCells[CurrentPos].Info != Empty && H->TheCells[CurrentPos].Element != Key) { CurrentPos += 2 * ++CollisionNum - 1; if (CurrentPos >= H->TableSize) CurrentPos -= H->TableSize; } return CurrentPos;}void Insert(ElementType Key, HashTable H){ Position Pos; Pos = Find(Key, H); if (H->TheCells[Pos].Info != Legitimate) { H->TheCells[Pos].Info = Legitimate; H->TheCells[Pos].Element = Key; }}ElementType Retrieve(Position P, HashTable H){ if(H && H->TheCells[P].Info==Legitimate) return H->TheCells[P].Element; else return 0;}HashTable Rehash(HashTable H){ int i, OldSize; Cell *OldCells; OldCells = H->TheCells; OldSize = H->TableSize; H = InitializeTable(2 * OldSize); for (i = 0; i < OldSize; i++) if (OldCell[i].Info == Legitimate) Insert(OldCells[i].Element, H); free(OldCells); return H;}
0 0
- 开放定址散列表--C语言实现
- 散列表的C语言实现-开放定址法
- 开放定址散列表(线性探测法)实现文件C语言
- 开放定址散列表(平方探测法)实现文件C语言
- 开放定址散列表(线性探测法(双散列))实现文件C语言
- 再散列开放定址散列表(线性探测法)加强版可再散列经优化实现文件C语言
- 散列表的实现-开放定址法
- 开放定址法实现散列表
- 散列 开放定址法 C实现
- 开放定址散列表(线性探测法)头文件C语言
- 开放定址散列表(平方探测法)头文件C语言
- 开放定址散列表(线性探测法(双散列))头文件C语言
- 再散列--开放定址散列表(线性探测法)加强版可再散列经优化头文件C语言
- 开放定址法解决hash冲突问题(C语言实现)
- 数据结构与算法分析-开放定址散列表的实现
- 散列表之开放定址法
- 开放定址平方探测散列表
- 散列表开放定址法的初始化、插入、打印等函数(c)
- 视频文件(任意文件)二进制读写
- android studio git设置忽略
- HDU-Red and Black
- 技术架构选型
- java性能优化技巧
- 开放定址散列表--C语言实现
- IOS学习日志(OC)1.5
- 关于设置虚拟主机及URL美化
- 模板元编程
- 【cas】keytool error: java.io.FileNotFoundException: xxx (Is a directory)
- LeetCode : 258. Add Digits
- intel芯片的虚拟化技术支持 vt-x开启
- 浅谈移动端网络优化
- Google和eBay在建设微服务生态系统中的深刻教训