当前位置:网站首页>哈希表及完整注释
哈希表及完整注释
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++;
}
}
边栏推荐
- CDB PDB user rights management
- Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
- 4--新唐nuc980 挂载initramfs nfs文件系统
- Chang'an chain learning notes - certificate model of certificate research
- fasterxml ToStringSerializerBase报错
- How do I dump SoapClient requests for debugging- How to dump SoapClient request for debug?
- FLIR blackfly s usb3 industrial camera: how to use counters and timers
- 【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
- 安德鲁斯—-多媒体编程
- Application analysis of face recognition
猜你喜欢

FLIR blackfly s usb3 industrial camera: how to use counters and timers

如何从0到1构建32Core树莓派集群

postgresql之整體查詢大致過程

Summer Challenge database Xueba notes (Part 2)~

牛客编程题--必刷101之双指针篇

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient

Compress JS code with terser

低代码平台中的数据连接方式(上)

Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!

Draco - gltf model compression tool
随机推荐
unity webgl自适应网页尺寸
STM32项目 -- 选题分享(部分)
Data connection mode in low code platform (Part 1)
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
导数、偏导数、方向导数
Rethinking of investment
FLIR blackfly s usb3 industrial camera: how to use counters and timers
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
A new path for enterprise mid Platform Construction -- low code platform
postgresql之整體查詢大致過程
leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Chang'an chain learning notes - certificate model of certificate research
Work of safety inspection
C#/VB.NET 删除Word文檔中的水印
ODBC database connection of MFC windows programming [147] (with source code)
Untiy文本框的代码换行问题
Gee upgrade can realize one piece of run tasks
电气工程及其自动化