当前位置:网站首页>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
边栏推荐
- This morning, investors began to travel collectively
- What is the difference between tolocal8bit and toutf8() in QT
- MySQL数据库查询优化
- 小学期,第三场-下午:WEB_sessionlfi
- KubeVela 1.4:让应用交付更安全、上手更简单、过程更透明
- qt中toLocal8Bit和toUtf8()有什么区别
- Build document editor based on slate
- 【NLP】【TextCNN】 文本分类
- Why should offline stores do new retail?
- Solution to rollback of MySQL database by mistake deletion
猜你喜欢
8 - function
4.3寸触控屏12路控制端口可编程网络中控支持5台中控主机相互备份
25:第三章:开发通行证服务:8:【注册/登录】接口:接收并校验“手机号和验证码”参数;(重点需要知道【利用redis来暂存数据,获取数据的】的应用场景)(使用到了【@Valid注解】参数校验)
What is the difference between tolocal8bit and toutf8() in QT
mysql统计账单信息(上):mysql安装及客户端DBeaver连接使用
微信小程序开发实战 云音乐
Torchdrug -- drug attribute prediction
Wechat applets - basics takes you to understand the life cycle of applets (2)
数据智能——DTCC2022!中国数据库技术大会即将开幕
Solution to rollback of MySQL database by mistake deletion
随机推荐
RP prototype resource sharing - shopping app
Basic syntax of VB
MySQL数据库误删回滚的解决
网易云签到可抽奖?那一年我能签到365天。不信?你看。
4.3-inch touch screen 12 channel control port programmable network central control supports mutual backup of 5 central control hosts
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
[multithreading] use the thread pool to implement a simple thread pool
8 - function
DELL R720服务器安装网卡Broadcom 5720驱动
composer
VB的基本语法
2022年高考都结束了,还有人真觉得程序员下班后不需要学习吗?
Conditional compilation
History, selection strategy and in-depth evaluation of note taking software
VR云展厅如何给线下实体带来活力?有哪些功能?
Idle fish is hard to turn over
This morning, investors began to travel collectively
Is it safe to open an account in Guangzhou stock exchange by mobile phone?
discuz 论坛提速之删除data/log下的xxx.php文件
MQ component (2022.5.16-5.22)