当前位置:网站首页>哈希表及完整注释
哈希表及完整注释
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++;
}
}
边栏推荐
- [C # notes] use file stream to copy files
- This week's hot open source project!
- Decryption function calculates "task state and lifecycle management" of asynchronous task capability
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- 【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
- Rethinking of investment
- 写作系列之contribution
- MetaForce原力元宇宙开发搭建丨佛萨奇2.0系统开发
- Chang'an chain learning notes - certificate model of certificate research
- Stm32f4 --- PWM output
猜你喜欢

阿里云易立:云原生如何破解企业降本提效难题?
![[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks](/img/06/17acf9958228cce5d80ada3275ad24.png)
[paper reading | deep reading] anrl: attributed network representation learning via deep neural networks

Alibaba cloud middleware open source past

进程管理基础

Infrared camera: juge infrared mag32 product introduction

猿桌派第三季开播在即,打开出海浪潮下的开发者新视野

Integerset of PostgreSQL

Web3's need for law

Douban average 9 x. Five God books in the distributed field!

Lombok同时使⽤@Data和@Builder 的坑
随机推荐
Use of fiddler
MySQL
postgresql之integerset
The empirical asset pricing package (EAP) can be installed through pypi
unity 自定义webgl打包模板
Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
Linear list --- circular linked list
fiddler的使用
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
【Node学习笔记】chokidar模块实现文件监听
【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
C#/VB.NET 删除Word文檔中的水印
Lidar: introduction and usage of ouster OS
leetcode:736. LISP syntax parsing [flowery + stack + status enumaotu + slots]
MySQL
[leetcode]Search for a Range
[C # notes] use file stream to copy files
6-6 vulnerability exploitation SSH security defense
QPushButton-》函数精解
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files