当前位置:网站首页>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 一致性哈希和虚拟节点
边栏推荐
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- TiDB数据迁移工具概览
- YoloV6训练:训练自己数据集遇到的各种问题
- Table responsive layout tips
- Arithmetic operations and related exercises in C language
- Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
- forEach的错误用法,你都学废了吗
- Makefile separates file names and suffixes
- 可视化搭建页面工具的前世今生
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
猜你喜欢
![[noi Simulation Competition] scraping (dynamic planning)](/img/ee/27a07f80207a2925f5065e633eb39f.png)
[noi Simulation Competition] scraping (dynamic planning)

Fatal: unsafe repository is owned by someone else

Factal: Unsafe repository is owned by someone else Solution

Base64 编码原来还可以这么理解

fatal: unsafe repository is owned by someone else 的解决方法

Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment

Socket and socket address

MathML to latex

Tmall product details interface (APP, H5 end)

. Net core logging system
随机推荐
LeetCode 2310. 个位数字为 K 的整数之和
C RichTextBox controls the maximum number of lines displayed
MFC A对话框调用B对话框函数并传参
Mfc a dialog calls B dialog function and passes parameters
MFC CString 转 char*
Obsidian installs third-party plug-ins - unable to load plug-ins
btrace-(字节码)动态跟踪工具
Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
2021-2022学年编译原理考试重点[华侨大学]
Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
Actual combat sharing of shutter screen acquisition
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Base64 coding can be understood this way
Reuse and distribution
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
AtCoder Beginner Contest 254
学习使用php将时间戳转换为大写日期的方法代码示例
Leetcode - Search 2D matrix
C语言习题---(数组)
【C语言】详解指针的初阶和进阶以及注意点(1)