当前位置:网站首页>07_哈希
07_哈希
2022-07-02 12:00:00 【听*雨声】
哈希
什么是哈希:
哈希:hash;就是散列函数
1 哈希冲突的4种处理方法:
- 1.开放定址法:也叫线性探测法;
一次探测缺点就是数据会集中在一块,解决方法就是平方探测(二次探测),避免堆积
正数:代表向右探测
负数:代表向左探测
2.再散列函数(哈希)法:
3.链地址法:
只要数据有冲突就头插在同一个下标下公共溢出区法:
一个基本表,一个溢出表;将有冲突的数据放入溢出表。
缺点是当数据过大,基本表太小时,溢出表的规格将会很大
2 链式哈希
结构定义:
#define HASHSIZE 12
typedef struct Node
{
int data;
struct Node* next;
}Node, * PNode;
typedef struct Head
{
//struct Node *arr[HASHSIZE];//指针数组(存放指针的数组)
struct Node arr[HASHSIZE];//数组arr中存放Node本身
}Head, * PHead;
API实现
void Init_listhash(PHead ph)
{
//assert
for (int i = 0; i < HASHSIZE; i++)
{
//ph->arr[i].data;// 不使用
ph->arr[i].next = NULL;
}
}
bool Insert_hash(PHead ph, int key)//头插
{
int hash = key % HASHSIZE;
//创建新节点
struct Node* pnewnode = (Node*)malloc(sizeof(Node) * 1);
assert(pnewnode != NULL);
pnewnode->data = key;
//插入
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)
{
//此时p->next 指向的这个节点就是我们要删除的那个节点
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 一致性哈希和虚拟节点
边栏推荐
- CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
- C语言中的printf函数和scanf函数
- Arithmetic operations and related exercises in C language
- 如何对 TiDB 进行 TPC-C 测试
- [C language] explain the initial and advanced levels of the pointer and points for attention (1)
- TiDB 软件和硬件环境建议配置
- C语言习题---(数组)
- Fundamentals of software testing
- Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
- Implement a server with multi process concurrency
猜你喜欢
Why can't programmers who can only program become excellent developers?
Btrace- (bytecode) dynamic tracking tool
Jenkins Pipeline 应用与实践
Fundamentals of software testing
LeetCode - 搜索二维矩阵
C#代码审计实战+前置知识
JMeter script parameterization
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
LeetCode 2320. Count the number of ways to place the house
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
随机推荐
AtCoder Beginner Contest 254
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
LeetCode 2320. 统计放置房子的方式数
STM32 standard firmware library function name memory (II)
Why can't browsers read JSX?
.NET Core 日志系统
2021-2022學年編譯原理考試重點[華僑大學]
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
广州市应急管理局发布7月高温高湿化工安全提醒
STM32 standard firmware library function name (I)
MFC timer usage
使用 TiUP 部署 TiDB 集群
MFC CString to char*
2021-2022学年编译原理考试重点[华侨大学]
Database connection pool and data source
About text selection in web pages and counting the length of selected text
实用调试技巧
C # delay, start the timer in the thread, and obtain the system time
TiDB数据迁移工具概览
Wechat applet uses towxml to display formula