当前位置:网站首页>散列表(哈希表)
散列表(哈希表)
2022-06-22 18:28:00 【单单一个越字】
哈希表的构造和冲突解决方法:
http://data.biancheng.net/view/63.html
其中二次探测法:d= 12,-12,……
哈希查找算法:
给定要查找的关键字key之后,先用过哈希函数找到key对应的地址,然后取该地址的值进行比较,如果不相等,说明在构造哈希表的过程中出现过冲突并采用过解决冲突的方法,这里假设采用的是开放定址法中的线性探测法。此时,也要通过线性探测法重新结算H(key),并进行比较。
代码:
#define HASHSIZE 12
#define NULLKEY -32768
typedef struct
{
int *elem; // 数据元素的基址,动态分配数组
int count; // 当前数据元素的个数
}HashTable;
int InitHashTable(HashTable *H)
{
H->count = HASHSIZE;
H->elem = (int *)malloc(HASHSIZE * sizeof(int));
if( !H->elem )
{
return -1;
}
for( i=0; i < HASHSIZE; i++ )
{
H->elem[i] = NULLKEY;
}
return 0;
}
// 使用除留余数法
int Hash(int key)
{
return key % HASHSIZE;
}
// 插入关键字到散列表
void InsertHash(HashTable *H, int key)
{
int addr;
addr = Hash(key);
while( H->elem[addr] != NULLKEY ) // 如果不为空,则冲突出现
{
addr = (addr + 1) % HASHSIZE; // 开放定址法的线性探测
}
H->elem[addr] = key;
}
// 散列表查找关键字
int SearchHash(HashTable H, int key, int *addr)
{
*addr = Hash(key);
while( H.elem[*addr] != key )
{
*addr = (*addr + 1) % HASHSIZE;
/* *addr == Hash(key)说明已经查找过一个周期也没找到, H.elem[*addr] == NULLKEY说明在最原始的addr往后依次查找时,遇到了空位,说明这个key并没有存放在此表中*/
if( H.elem[*addr] == NULLKEY || *addr == Hash(key) )
{
return -1;
}
}
return 0;
}
代码讲解:
B站小甲鱼视频:
https://www.bilibili.com/video/BV1jW411K7yg?p=87
边栏推荐
- Creator mode summary
- Velocity 语法
- 51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
- [nfs无法挂载问题] mount.nfs: access denied by server while mounting localhost:/data/dev/mysql
- 华为云招募工业智能领域合作伙伴,强力扶持+商业变现
- 详解openGauss多线程架构启动过程
- c# winform 嵌入flash
- AB打包有的Shader没有触发IPreprocessShaders的回调
- Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
- 第一章 力扣热题100道(1-5)
猜你喜欢

0816 shortcomings of Feida (improvement direction)

2. what is mechanical design?

详解openGauss多线程架构启动过程

1.2----- mechanical design tools (CAD software) and hardware design tools (EDA software) and comparison

Weizhi technology appeared in the Western Digital Expo, and the space-time AI technology was highly recognized

1.2-----机械设计工具(CAD软件)和硬件设计工具(EDA软件)及对比

0.1----- process of drawing PCB with AD

0816飞达的缺点(改进方向)

84. (cesium chapter) movement of cesium model on terrain

A homekit enabled camera? Zhiting IPC camera IC1 unpacking experience
随机推荐
NRF51822外设学习
Velocity syntax
what? Homekit, Micah, aqara and other ecosystems can also be linked with tmall elf ecology through zhiting?
Problems of different renderers running on the web with flutter2.0
A homekit enabled camera? Zhiting IPC camera IC1 unpacking experience
84. (cesium chapter) movement of cesium model on terrain
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
Human Pose Estimation浅述
给对象赋值
How to choose smart home? Take a look at this shopping guide
深入浅出聊布隆过滤器(Bloom Filter)
0816 shortcomings of Feida (improvement direction)
详解openGauss多线程架构启动过程
修改隐含参数造成SQL性能下降案例之二
自定义控件AutoScaleMode为Font造成宽度增加的问题
Xintang nuc980 usage record: basic description of development environment preparation and compilation configuration
第一篇 热身--隐式类型转换还是其他?
Recommend an anatomy website
K个一组翻转链表[链表拆解/翻转/拼装]