当前位置:网站首页>MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
2022-06-27 17:58:00 【KFZ433】
3.2 链地址法
链地址法就是将相应位置上冲突的所有关键词存储在同一个单链表中。
设关键字序列为 47 , 7 , 29 , 11 , 16 , 92 , 22 , 8 , 3 , 50 , 37 , 89 , 94 , 21 47, 7, 29, 11, 16, 92, 22, 8, 3, 50, 37, 89, 94, 2147,7,29,11,16,92,22,8,3,50,37,89,94,21,散列函数取为h ( k e y ) = k e y m o d 11 h(key) = key \mod 11h(key)=keymod11,用分离链接法处理冲突。
表中有9个结点只需1次查找,5个结点需要2次查找,所以查找成功的平均查找次数为:
A S L s = ( 9 + 5 ∗ 2 ) / 14 ≈ 1.36
参考代码:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <malloc.h>
using namespace std;
#define MAXTABLESIZE 10000 //允许开辟的最大散列表长度
#define KEYLENGTH 100 //关键字的最大长度
typedef int ElementType;
struct LNode
{
ElementType data;LNode *next;};
typedef LNode *PtrToNode;
typedef PtrToNode LinkList;
struct TblNode
{
int tablesize; //表的最大长度LinkList heads; //存放散列单元数据的数组};
typedef struct TblNode *HashTable;
/返回大于n且不超过MAXTABLESIZE的最小素数/
int NextPrime(int n)
{
int p = (n % 2) ? n + 2 : n + 1; //从大于n的下一个奇数开始int i;while (p <= MAXTABLESIZE){ for (i = (int)sqrt(p); i > 2; i--) { if ((p % i) == 0) break; } if (i == 2) break; //说明是素数,结束 else p += 2;}return p;}
/创建新的哈希表/
HashTable CreateTable(int table_size)
{
HashTable h = (HashTable)malloc(sizeof(TblNode));h->tablesize = NextPrime(table_size);h->heads = (LinkList)malloc(h->tablesize * sizeof(LNode));//初始化表头结点for (int i = 0; i < h->tablesize; i++){ h->heads[i].next = NULL;}return h;}
/查找数据的初始位置/
int Hash(ElementType key, int n)
{
//这里只针对大小写return key % 11;}
/查找元素位置/
LinkList Find(HashTable h, ElementType key)
{
int pos;pos = Hash(key, h->tablesize); //初始散列位置LinkList p = h->heads[pos].next; //从链表的第一个节点开始while (p && key != p->data){ p = p->next;}return p;}
/插入新的元素/
bool Insert(HashTable h, ElementType key)
{
LinkList p = Find(h, key); //先查找key是否存在if (!p){ //关键词未找到,可以插入 LinkList new_cell = (LinkList)malloc(sizeof(LNode)); new_cell->data = key; int pos = Hash(key, h->tablesize); new_cell->next = h->heads[pos].next; h->heads[pos].next = new_cell; return true;}else{ cout << "键值已存在!" << endl; return false;}}
/销毁链表/
void DestroyTable(HashTable h)
{
int i;LinkList p, tmp;//释放每个节点for (i = 0; i < h->tablesize; i++){ p = h->heads[i].next; while (p) { tmp = p->next; free(p); p = tmp; }}free(h->heads);free(h);}
int main(int argc, char const *argv[])
{
int a[] = {47, 7, 29,29, 11, 16, 92, 22, 8, 3, 50, 37, 89, 94, 21};int n = 15;HashTable h = CreateTable(n);for (int i = 0; i < n; i++){ Insert(h, a[i]); //插入元素}for (int i = 0; i < h->tablesize; i++){ LinkList p = h->heads[i].next; while (p) { cout << p->data << " "; //打印哈希表元素 p = p->next; } cout << endl;}return 0;}
边栏推荐
- Function key input experiment based on stm32f103zet6 Library
- Introduction to deep learning and neural networks
- 云笔记到底哪家强 -- 教你搭建自己的网盘服务器
- 流程判断-三目运算-for循环
- SQL Server - Window Function - 解决连续N条记录过滤问题
- 高收益银行理财产品在哪里看?
- Substrate及波卡一周技术更新速递 20220425 - 20220501
- 在线文本按行批量反转工具
- 《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
- Labelimg usage guide
猜你喜欢

Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million

华大单片机KEIL报错_WEAK的解决方案

拥抱云原生:江苏移动订单中心实践

Blink SQL内置函数大全

華大單片機KEIL報錯_WEAK的解决方案

International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding

国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)

华大单片机KEIL添加ST-LINK解决方法

Crawl national laws and Regulations Database

binder hwbinder vndbinder
随机推荐
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
教你打印自己的日志 -- 如何自定义 log4j2 各组件
A simple calculation method of vanishing point
基础数据类型和复杂数据类型
[cloud based co creation] the "solution" of Digital Travel construction in Colleges and Universities
图扑数字孪生智慧能源一体化管控平台
这个是和数据采集一样,可以定义一个参数为上个月或者前一天,然后在sql中使用这个参数吗?
Crawl national laws and Regulations Database
Buzzer experiment based on stm32f103zet6 library function
Solution to Maxwell error (MySQL 8.x connection)
redis集群系列三
什么是SSR/SSG/ISR?如何在AWS上托管它们?
Don't worry. This is the truth about wages in all industries in China
UE4-Actor基础知识
如何利用 RPA 实现自动化获客?
让单测变得如此简单 -- spock 框架初体验
在线文本按行批量反转工具
数仓的字符截取三胞胎:substrb、substr、substring
循环遍历及函数基础知识
Solution of adding st-link to Huada MCU Keil