当前位置:网站首页>Hash table and full comments
Hash table and full comments
2022-07-07 02:36:00 【@Landscape post yuan】
#include "stdio.h"
#include "stdlib.h"
typedef struct{
int* elem; // Data element storage base address , Assign arrays dynamically
int count; // Number of current data elements
}HashTable;
int m=0; // Hash table length , Global variables
//HASHSIZE Define the hash table length as the length of the array
typedef enum { // Or not typedef, take Status On the front , The function called is preceded by enum
OK=1,SUCCESS=1,UNSUCCESS=0,HASHSIZE=12,NULLKEY=-32768
}Status;
Status InitHashTable(HashTable *H){ // Initialize hash table
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){ // Hash function key amount to qq account number
return key%m; // Division and remainder Return key
}
void InsertHash(HashTable* H,int key){ // Insert keywords into hash table
int addr=Hash(key); // Find the hash address
printf("The subscript of the inserted array position:%d\n",addr);
while(H->elem[addr]!=NULLKEY) // If it's not empty , Then conflict
addr=(addr+1)%m; // Develop linear detection of addressing
H->elem[addr]=key; // Until there is a space to insert keywords
}
Status SearchHash(HashTable H,int key,int* addr){ // Insert keywords into hash table
*addr=Hash(key); // Find the hash address
while(H.elem[*addr]!=key){ // If it's not empty , Then conflict
*addr=(*addr+1)%m; // Develop linear detection of addressing
if(H.elem[*addr]==NULLKEY||*addr==Hash(key)){
// If the loop goes back to the origin
// Key does not exist
return UNSUCCESS;
}
}
return SUCCESS;
}
int main(){
int i=0;
HashTable H;
InitHashTable(&H);
while(i<HASHSIZE){
InsertHash(&H,i); // Insert 12 Number
i++;
}
int key=0;
int addr; // Return the function of subscript
while(key<HASHSIZE){
if(SearchHash(H,key,&addr)==SUCCESS){ // Search the position of this number
printf("Print the position of this number:%d\n",addr);
} else{
printf("UNSUCCESS\n");
}
key++;
}
}
边栏推荐
- wzoi 1~200
- MFC Windows 程序设计[147]之ODBC数据库连接(附源码)
- unity 自定义webgl打包模板
- [unity notes] screen coordinates to ugui coordinates
- Rethinking of investment
- #夏日挑战赛#数据库学霸笔记(下)~
- Difference and the difference between array and array structure and linked list
- 1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
- Data connection mode in low code platform (Part 1)
- Lumion 11.0 software installation package download and installation tutorial
猜你喜欢
AWS学习笔记(一)
本周 火火火火 的开源项目!
导数、偏导数、方向导数
[paper reading | deep reading] rolne: improving the quality of network embedding with structural role proximity
MySQL --- 常用函数 - 字符串函数
基于ensp防火墙双击热备二层网络规划与设计
FLIR blackfly s usb3 industrial camera: how to use counters and timers
慧通编程入门课程 - 2A闯关
C语言练习题_1
Infrared camera: juge infrared mag32 product introduction
随机推荐
MySQL
[leetcode]Search for a Range
MMDetection3D加载毫米波雷达数据
postgresql之整体查询大致过程
postgresql 之 数据目录内部结构 简介
数字滚动增加效果
Ali yunyili: how does yunyuansheng solve the problem of reducing costs and improving efficiency?
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
Pioneer of Web3: virtual human
C语言练习题_1
#夏日挑战赛#数据库学霸笔记(下)~
postgresql之integerset
Why am I warned that the 'CMAKE_ TOOLCHAIN_ FILE' variable is not used by the project?
Safety delivery engineer
[unity notes] screen coordinates to ugui coordinates
C # / vb. Net supprime le filigrane d'un document word
[paper reading | deep reading] dngr:deep neural networks for learning graph representations
4--新唐nuc980 挂载initramfs nfs文件系统
Go swagger use
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野