当前位置:网站首页>07_ Hash
07_ Hash
2022-07-02 15:13:00 【Listen to the rain】
Hash
What is hash :
Hash :hash; It's a hash function
1 Hash conflicting 4 Method of disposal :
- 1. Open addressing : Also called linear detection ;
The disadvantage of one detection is that the data will be concentrated in one piece , The solution is square detection ( Second detection ), Avoid stacking
Positive numbers : Represents right probe
negative : Represents left probe
2. Hash function again ( Hash ) Law :
3. Chain address :
As long as there is data conflict, the header is inserted under the same subscriptPublic spill area law :
A basic table , An overflow table ; Put the conflicting data into the overflow table .
The disadvantage is when the data is too large , The basic table is too small , The specification of overflow table will be very large
2 Chained hash
Structure definition :
#define HASHSIZE 12
typedef struct Node
{
int data;
struct Node* next;
}Node, * PNode;
typedef struct Head
{
//struct Node *arr[HASHSIZE];// Pointer array ( An array of pointers )
struct Node arr[HASHSIZE];// Array arr In the store Node In itself
}Head, * PHead;
API Realization
void Init_listhash(PHead ph)
{
//assert
for (int i = 0; i < HASHSIZE; i++)
{
//ph->arr[i].data;// Don't use
ph->arr[i].next = NULL;
}
}
bool Insert_hash(PHead ph, int key)// Head insertion
{
int hash = key % HASHSIZE;
// Create a new node
struct Node* pnewnode = (Node*)malloc(sizeof(Node) * 1);
assert(pnewnode != NULL);
pnewnode->data = key;
// Insert
pnewnode->next = ph->arr[hash].next;
ph->arr[hash].next = pnewnode;
return true;
}
bool Del_Hash(PHead ph, int key)
{
int hash = key % HASHSIZE;
struct Node* p = &ph->arr[hash];
for (p; p->next != NULL; p = p->next)
{
if (p->next->data == key)
{
// here p->next The node pointed to is the node we want to delete
struct Node* q = p->next;
p->next = q->next;
free(q);
q = NULL;
return true;
}
}
return false;
}
struct Node* Search(PHead ph, int key)
{
int hash = key % HASHSIZE;
struct Node* p = ph->arr[hash].next;
for (p; p != NULL; p = p->next)
{
if (p->data == key)
{
return p;
}
}
return NULL;
}
void Show_Hash(PHead ph)
{
for (int i = 0; i < HASHSIZE; i++)
{
printf("%3d: ", i);
struct Node* p = ph->arr[i].next;
for (p; p != NULL; p = p->next)
{
printf("%d ", p->data);
}
printf("\n");
}
}
3 Consistent hashes and virtual nodes
边栏推荐
- Jenkins Pipeline 应用与实践
- info [email protected] : The platform “win32“ is incompatible with this module.
- Internet Explorer officially retired
- 蜻蜓低代码安全工具平台开发之路
- Have you learned the wrong usage of foreach
- 学习使用php实现公历农历转换的方法代码
- LeetCode 2320. Count the number of ways to place the house
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- Practical debugging skills
- geoserver离线地图服务搭建和图层发布
猜你喜欢
随机推荐
Ad20 cannot select the solution of component packaging in PCB editor
Base64 coding can be understood this way
03_线性表_链表
19_Redis_宕机后手动配置主机
Mfc a dialog calls B dialog function and passes parameters
871. 最低加油次数 : 简单优先队列(堆)贪心题
LeetCode 2320. Count the number of ways to place the house
Map介绍
学习使用php将时间戳转换为大写日期的方法代码示例
Application and practice of Jenkins pipeline
Recommended configuration of tidb software and hardware environment
Deploy tidb cluster with tiup
SQL 后计算的利器 SPL
LeetCode - 搜索二维矩阵
数据库内容输出有问题怎么解决
geoserver离线地图服务搭建和图层发布
21_ Redis_ Analysis of redis cache penetration and avalanche
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
MFC timer usage
2021-2022学年编译原理考试重点[华侨大学]