当前位置:网站首页>哈希表及完整注释
哈希表及完整注释
2022-07-06 19:00:00 【@风景邮递Yuan】
#include "stdio.h"
#include "stdlib.h"
typedef struct{
int* elem; //数据元素存储基址,动态分配数组
int count; //当前数据元素个数
}HashTable;
int m=0; //散列表表长,全局变量
//HASHSIZE 定义散列表长为数组的长度
typedef enum { //或者不要typedef,将Status放在前面,调用的函数前面加enum
OK=1,SUCCESS=1,UNSUCCESS=0,HASHSIZE=12,NULLKEY=-32768
}Status;
Status InitHashTable(HashTable *H){ //初始化散列表
int i;
m=HASHSIZE;
H->count=m;
H->elem=(int *)malloc(m*sizeof(int));
for(i=0;i<m;i++)
H->elem[i]=NULLKEY;
return OK;
}
int Hash(int key){ //散列函数 key相当于qq账号
return key%m; //除留余数法 返回钥匙
}
void InsertHash(HashTable* H,int key){ //插入关键字进散列表
int addr=Hash(key); //求散列地址
printf("The subscript of the inserted array position:%d\n",addr);
while(H->elem[addr]!=NULLKEY) //如果不为空,则冲突
addr=(addr+1)%m; //开发定址法的线性探测
H->elem[addr]=key; //直到有空位后插入关键字
}
Status SearchHash(HashTable H,int key,int* addr){ //插入关键字进散列表
*addr=Hash(key); //求散列地址
while(H.elem[*addr]!=key){ //如果不为空,则冲突
*addr=(*addr+1)%m; //开发定址法的线性探测
if(H.elem[*addr]==NULLKEY||*addr==Hash(key)){
//如果循环回到原点
//则说明关键字不存在
return UNSUCCESS;
}
}
return SUCCESS;
}
int main(){
int i=0;
HashTable H;
InitHashTable(&H);
while(i<HASHSIZE){
InsertHash(&H,i); //插入12个数
i++;
}
int key=0;
int addr; //返回下标的作用
while(key<HASHSIZE){
if(SearchHash(H,key,&addr)==SUCCESS){ //搜索到了这个数的位置
printf("Print the position of this number:%d\n",addr);
} else{
printf("UNSUCCESS\n");
}
key++;
}
}
边栏推荐
- STM32 project -- Topic sharing (part)
- Douban average 9 x. Five God books in the distributed field!
- How to build a 32core raspberry pie cluster from 0 to 1
- 差异与阵列和阵列结构和链表的区别
- Lombok makes the pit of ⽤ @data and @builder at the same time
- MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
- Use of pgpool II and pgpooladmin
- The empirical asset pricing package (EAP) can be installed through pypi
- Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
- 低代码平台中的数据连接方式(上)
猜你喜欢
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
MATLB|具有储能的经济调度及机会约束和鲁棒优化
软件测试——Jmeter接口测试之常用断言
C#/VB. Net to delete watermarks in word documents
低代码平台中的数据连接方式(上)
Linear list --- circular linked list
Infrared camera: juge infrared mag32 product introduction
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
压缩 js 代码就用 terser
如何从0到1构建32Core树莓派集群
随机推荐
PostgreSQL图形化界面工具之pgAdmin4
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
postgresql 之 数据目录内部结构 简介
A new path for enterprise mid Platform Construction -- low code platform
1--新唐nuc980 NUC980移植 UBOOT,从外部mx25l启动
【论文阅读|深读】RolNE: Improving the Quality of Network Embedding with Structural Role Proximity
#夏日挑战赛#数据库学霸笔记(下)~
Detailed explanation of line segment tree (including tested code implementation)
Alibaba cloud middleware open source past
【Node学习笔记】chokidar模块实现文件监听
[xlua notes] array of lua to array of C #
6-6漏洞利用-SSH安全防御
3--新唐nuc980 kernel支持jffs2, Jffs2文件系统制作, 内核挂载jffs2, uboot网口设置,uboot支持tftp
用全连接+softmax对图片的feature进行分类
Halcon实例转OpenCvSharp(C# OpenCV)实现--瓶口缺陷检测(附源码)
Rethinking of investment
The cities research center of New York University recruits master of science and postdoctoral students
Common fitting models and application methods of PCL
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Lombok makes the pit of ⽤ @data and @builder at the same time