当前位置:网站首页>C language: hashtable
C language: hashtable
2022-06-30 20:08:00 【B! GGer.】
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*Func)(int, int);
// Zipper method
// List nodes , Save string in data field
typedef struct Node{
Func func;
struct Node *next;
}Node;
typedef struct
{
char *funcName;
Func func;
}funcTable;
int add(int a, int b)
{
printf("add\n");
return a+b;
}
int sub(int a, int b)
{
printf("sub\n");
return a-b;
}
// The order sheet ( Header , size )
typedef struct HashTable{
// List header address , It is the sequence table of the chain header nodes , Because the header node is Node *, Sequential representation Node **
Node **data;
int size;
}HashTable;
// Initialize linked list , The first interpolation , Of the new node next = Incoming node
Node *init_node(Func func, Node *head){
Node *p = (Node *)malloc(sizeof(Node));
p->func = func;
p->next = head;
return p;// Virtual head node
}
HashTable *init_hashtable(int n){
HashTable *h = (HashTable *)malloc(sizeof(HashTable));
//h->size = n << 1;// To make hash table storage more efficient , Double the storage space ??
h->size = n;
h->data = (Node **)calloc(h->size, sizeof(Node *));
return h;
}
void clear_node(Node *node){
if (node == NULL) return;
Node *p = node, *q;
while(p){
q = p->next;
free(p);
p = q;
}
free(q);//?
return;
}
void clear_hashtable(HashTable *h){
if (h == NULL) return;
for (int i = 0; i < h->size; i++){
clear_node(h->data[i]);
}
free(h->data);
free(h);
return;
}
int BKDRHash(char *str){
int seed = 31, hash = 0;
for (int i = 0; str[i]; i++) hash = hash * seed + str[i];
// Ensure positive numbers
return hash & 0x7fffffff;
}
int insert(HashTable *h, char *str, Func func){
int hash = BKDRHash(str);
int ind = hash % h->size;
printf("ind:%d\n", ind);
h->data[ind] = init_node(func, h->data[ind]);
return 1;
}
Func search(HashTable *h, char *str){
int hash = BKDRHash(str);
int ind = hash % h->size;
Node *p = h->data[ind];
//while (p && strcmp(str, p->str)) p = p->next;
if (p != NULL)
{
return p->func;
}
return NULL;
}
static funcTable gfuncTable[] = {
{"func_add", add},
{"test1", NULL},
{"test2", NULL},
{"test3", NULL},
{"func_sub", sub},
{"test4", NULL},
{"test5", NULL},
{"test6", NULL},
};
int main(){
HashTable *h = init_hashtable(sizeof(gfuncTable)/sizeof(funcTable));
for (int i = 0; i < sizeof(gfuncTable)/sizeof(funcTable); i++)
{
insert(h, gfuncTable[i].funcName, gfuncTable[i].func);
}
Func funcAdd = search(h, "func_add");
printf("add:%d\n", funcAdd(1, 2));
Func funcSub = search(h, "func_sub");
printf("sub:%d\n", funcSub(7, 2));
return 0;
}
Reference link :https://blog.csdn.net/one_chow_chow/article/details/109561575
边栏推荐
- 达梦数据库重新初始化实例操作记录
- 1. 爬虫之Beautifulsoup解析库&在线解析图片验证码
- 神经网络入门(上)
- Why should offline stores do new retail?
- 【ICLR 2021】半监督目标检测:Unbiased Teacher For Semi-Supervised Object Detection
- Ten percent of the time, the tar command can't parse the English bracket "()" when decompressing the file
- 重复乃技艺之母
- mysql主从同步
- 软件工程最佳实践——项目需求分析
- 腾讯会议应用市场正式上线,首批入驻超20款应用
猜你喜欢

Data intelligence - dtcc2022! China database technology conference is about to open

Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)

GeoServer安装

【Try to Hack】Windows系统账户安全

Character class of regular series

CADD course learning (1) -- basic knowledge of drug design

派尔特医疗在港交所招股书二次“失效”,上市计划实质性延迟

昔日果汁大王,16个亿卖了

Why should offline stores do new retail?
Solution to rollback of MySQL database by mistake deletion
随机推荐
暑期实训21组第一周个人工作总结
如何快速通过PMP考试?
Detailed explanation of specific methods and steps for TCP communication between s7-1500 PLCs (picture and text)
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Application of JDBC in performance test
Source code analysis of redis ziplist compressed list
[ICLR 2021] semi supervised object detection: unbiased teacher for semi supervised object detection
CADD课程学习(2)-- 靶点晶体结构信息
广州股票开户选择手机办理安全吗?
Detailed steps for Django to upload excel tables and write data to the database
DELL R720服务器安装网卡Broadcom 5720驱动
操作系统面试题汇总(不定期更新)
Growth summer challenge is coming, exclusive community welfare is coming ~ get CSDN customized T-shirt for free
pytorch实现FLOPs和Params的计算
小学期,第三场-下午:WEB_sessionlfi
Kubernetes为什么会赢,容器圈的风云变幻!
pycharm从安装到全副武装,学起来才嗖嗖的快,图片超多,因为过度详细!
Primary school, session 3 - afternoon: Web_ sessionlfi
神经网络入门(上)
英语没学好到底能不能做coder,别再纠结了先学起来