当前位置:网站首页>Mass lucky hash game system development - conflict resolution (code analysis)
Mass lucky hash game system development - conflict resolution (code analysis)
2022-06-27 19:57:00 【KFZ433】
3.2 Chain address
The chain address method is to store all the conflicting keywords in the corresponding positions in the same single chain table .
Set the keyword sequence to 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, The hash function is taken as h ( k e y ) = k e y m o d 11 h(key) = key \mod 11h(key)=keymod11, Deal with conflicts by separating links .
Table has 9 Nodes only need 1 Search times ,5 Nodes need 2 Search times , So the average number of successful searches is :
A S L s = ( 9 + 5 ∗ 2 ) / 14 ≈ 1.36
Reference code :
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <malloc.h>
using namespace std;
#define MAXTABLESIZE 10000 // Maximum hash table length allowed
#define KEYLENGTH 100 // The maximum length of a keyword
typedef int ElementType;
struct LNode
{
ElementType data;LNode *next;};
typedef LNode *PtrToNode;
typedef PtrToNode LinkList;
struct TblNode
{
int tablesize; // The maximum length of the table LinkList heads; // An array of hash cell data };
typedef struct TblNode *HashTable;
/ Return is greater than the n And no more than MAXTABLESIZE The minimum prime number of /
int NextPrime(int n)
{
int p = (n % 2) ? n + 2 : n + 1; // From greater than n Start with the next odd number int i;while (p <= MAXTABLESIZE){ for (i = (int)sqrt(p); i > 2; i--) { if ((p % i) == 0) break; } if (i == 2) break; // The explanation is prime , end else p += 2;}return p;}
/ Create a new hash table /
HashTable CreateTable(int table_size)
{
HashTable h = (HashTable)malloc(sizeof(TblNode));h->tablesize = NextPrime(table_size);h->heads = (LinkList)malloc(h->tablesize * sizeof(LNode));// Initialize the header node for (int i = 0; i < h->tablesize; i++){ h->heads[i].next = NULL;}return h;}
/ Find the initial location of the data /
int Hash(ElementType key, int n)
{
// This is only for case return key % 11;}
/ Find element location /
LinkList Find(HashTable h, ElementType key)
{
int pos;pos = Hash(key, h->tablesize); // Initial hash position LinkList p = h->heads[pos].next; // Start with the first node of the list while (p && key != p->data){ p = p->next;}return p;}
/ Insert new elements /
bool Insert(HashTable h, ElementType key)
{
LinkList p = Find(h, key); // To find the first key Whether there is if (!p){ // Keyword not found , You can insert 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 << " Key value already exists !" << endl; return false;}}
/ Destroy the list /
void DestroyTable(HashTable h)
{
int i;LinkList p, tmp;// Release each node 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]); // Insert elements }for (int i = 0; i < h->tablesize; i++){ LinkList p = h->heads[i].next; while (p) { cout << p->data << " "; // Print hash table elements p = p->next; } cout << endl;}return 0;}
边栏推荐
- Rust 中的枚举和控制流运算
- Rust advanced ownership - memory management
- What is ssr/ssg/isr? How do I host them on AWS?
- (LC)46. Full Permutation
- ABAP-CL_OBJECT_COLLECTION工具类
- Manage rust project through cargo
- 1025 PAT Ranking
- Structs in trust
- 429- binary tree (108. convert the ordered array into a binary search tree, 538. convert the binary search tree into an accumulation tree, 106. construct a binary tree from the middle order and post o
- Leetcode 989. 数组形式的整数加法(简单)
猜你喜欢

海底电缆探测技术总结

Crontab's learning essays

429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)

GIS遥感R语言学习看这里

Data intelligence enters the "deep water area", and data governance is the key

Data intelligence enters the "deep water area", and data governance is the key

crontab的学习随笔

可观测,才可靠:云上自动化运维CloudOps系列沙龙 第一弹

Array exercises follow up
![[debug] platform engineering interface debugging](/img/bc/ec630358b039c2a9551b7ae99d7fb3.png)
[debug] platform engineering interface debugging
随机推荐
One to one relationship
MASS幸运哈希游戏系统开发丨冲突解决方法(代码分析)
Enumeration and control flow operation in rust
UE4随笔:FString、FName 与 FText
A simple calculation method of vanishing point
UE4:Build Configuration和Config的解释
判断一个变量是数组还是对象?
External interrupt experiment based on stm32f103zet6 library function
券商经理的开户二维码开户买股票安全吗?有谁知道啊
1024 Palindromic Number
散列表(Hash)-复习
刷题笔记-树(Easy)-更新中
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Pointers and structs
UE4-Actor基础知识
shell脚本常用命令(三)
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
刷题记录:Easy 数组(持续更新)
Rust 所有权进阶 -- 内存管理
Solution of adding st-link to Huada MCU Keil