当前位置:网站首页>散列表(哈希表)
散列表(哈希表)
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
边栏推荐
- 1.4----- PCB design? (circuit design) determination scheme
- Digital supply chain centralized purchase platform solution for mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
- Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
- Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)
- 84.(cesium篇)cesium模型在地形上运动
- 【干货|接口测试必备技能-常见接口协议解析】
- 给对象赋值
- Assign values to objects
- 推荐一个解剖学网站
- 0.1----- process of drawing PCB with AD
猜你喜欢

Openpnp调试 ------ 0816飞达推0402编带

Xintang nuc980 usage record: basic description of development environment preparation and compilation configuration

NLP-D57-nlp比赛D26&刷题D13&读论文&&找了一个多小时bug

Activereports report practical application tutorial (19) -- multi data source binding

远程访问及控制——SSH远程管理及TCP Wrappers 访问控制

Shell script explanation (IV) -- while loop and until loop of loop statements (additional examples and analysis)

NRF51822外设学习

Chapter I 100 hot questions (1-5)
![[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql](/img/15/cbb95ec823cdde5fb8f032dc45cfc7.png)
[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql

技术管理进阶——你了解成长的全貌吗?
随机推荐
Agent model of structured model
delegate
小波变换db4进行四层分解及其信号重构—matlab分析及C语言实现
08_ One word sobers you up
ABAQUS 使用RSG绘制插件初体验
如何用银灿IS903主控DIY自己的U盘?(练习BGA焊接的好项目)
故障分析 | 从 data_free 异常说起
Human pose estimation
Flutter series -flutter route management
How to choose smart home? Take a look at this shopping guide
510000 prize pool invites you to join the war! The second Alibaba cloud ECS cloudbuild developer competition is coming
vim中快速缩进用法
界面开发组件DevExpress ASP.NET Core v21.2 - UI组件增强
Interface development component devaxpress asp Net core v21.2 - UI component enhancements
Solution of off grid pin in Altium Designer
修改隐含参数造成SQL性能下降案例之二
详解openGauss多线程架构启动过程
Ts as const
0.0 - how can SolidWorks be uninstalled cleanly?
Explain in simple terms the bloom filter