当前位置:网站首页>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
边栏推荐
- dataloader 源码_DataLoader
- 屏幕显示技术进化史
- Advanced skills of testers: a guide to the application of unit test reports
- 广州股票开户选择手机办理安全吗?
- Filebeat自定义index和fields
- Django上传excel表格并将数据写入数据库的详细步骤
- Idle fish is hard to turn over
- pytorch实现FLOPs和Params的计算
- 6-1漏洞利用-FTP漏洞利用
- How does an in memory database take advantage of memory?
猜你喜欢

Character class of regular series

The prospectus of pelt medical was "invalid" for the second time in the Hong Kong stock exchange, and the listing plan was substantially delayed

8 - 函数

Summary of operating system interview questions (updated from time to time)

2022年高考都结束了,还有人真觉得程序员下班后不需要学习吗?

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

Tencent conference application market was officially launched, with more than 20 applications in the first batch

Advanced skills of testers: a guide to the application of unit test reports

正则系列之字符类

软件工程最佳实践——项目需求分析
随机推荐
CV+Deep Learning——网络架构Pytorch复现系列——basenets(BackBones)(一)
派尔特医疗在港交所招股书二次“失效”,上市计划实质性延迟
腾讯会议应用市场正式上线,首批入驻超20款应用
【ICLR 2021】半监督目标检测:Unbiased Teacher For Semi-Supervised Object Detection
Why must a digital transformation strategy include continuous testing?
How unity pulls one of multiple components
【NLP】【TextCNN】 文本分类
计网 | 【五 传输层、六 应用层】知识点及例题
正则系列之字符类
qt中toLocal8Bit和toUtf8()有什么区别
Django上传excel表格并将数据写入数据库的详细步骤
软件工程最佳实践——项目需求分析
Why should offline stores do new retail?
QT :QAxObject操作Excel
Why must we move from Devops to bizdevops?
WeakSet
如何快速通过PMP考试?
8 - function
[solved] how does Tiktok cancel paying attention to the cancelled account
Graduates