当前位置:网站首页>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
边栏推荐
- 条件编译
- 英语没学好到底能不能做coder,别再纠结了先学起来
- CADD course learning (2) -- target crystal structure information
- 广州股票开户选择手机办理安全吗?
- SM2246EN+闪迪15131
- 达梦数据库重新初始化实例操作记录
- MySQL数据库查询优化
- Kubevela 1.4: make application delivery safer, easier to use, and more transparent
- [solved] how does Tiktok cancel paying attention to the cancelled account
- 更智能!AIRIOT加速煤炭行业节能减排升级
猜你喜欢

Wechat applets - basics takes you to understand the life cycle of applets (2)

Buttons to achieve various effects and functions. Reading this article is enough

Torchdrug -- drug attribute prediction

25:第三章:开发通行证服务:8:【注册/登录】接口:接收并校验“手机号和验证码”参数;(重点需要知道【利用redis来暂存数据,获取数据的】的应用场景)(使用到了【@Valid注解】参数校验)

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

Tensorflow2.4实现RepVGG

Cartoon | has Oracle been abandoned by the new era?

网易云签到可抽奖?那一年我能签到365天。不信?你看。

RP原型资源分享-购物类App

操作系统面试题汇总(不定期更新)
随机推荐
暑期实训21组第一周个人工作总结
Lombok
Conditional compilation
8 - function
Primary school, session 3 - afternoon: Web_ sessionlfi
“更福特、更中国”拨云见日,长安福特王牌产品订单过万
超视频时代的音视频架构建设|Science和英特尔联袂推出“架构师成长计划”第二季
基于slate构建文档编辑器
Taiwan SSS Xinchuang sss1700 replaces cmedia cm6533 24bit 96KHz USB audio codec chip
Application of JDBC in performance test
Data intelligence - dtcc2022! China database technology conference is about to open
PS2手柄-1「建议收藏」
QQmlApplicationEngine failed to load component qrc:/main.qml:-1 No such file or directory
VR全景添加对比功能,让差异化效果展示更直观!
GeoServer安装
Kubevela 1.4: make application delivery safer, easier to use, and more transparent
This morning, investors began to travel collectively
一文读懂目标检测:R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD「建议收藏」
微信小程序开发实战 云音乐
【ICLR 2021】半监督目标检测:Unbiased Teacher For Semi-Supervised Object Detection