当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
composer
当我们在看待产业互联网的时候,总是会站在消费互联网的对立面来看待它
TorchDrug--药物属性预测
Client请求外部接口标准处理方式
Lombok
Enterprise middle office planning and it architecture microservice transformation
无线充U型超声波电动牙刷方案开发
DNS服务器搭建、转发、主从配置
台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
C语言:hashTable
Playwright - 滚动条操作
Detailed explanation of specific methods and steps for TCP communication between s7-1500 PLCs (picture and text)
[multithreading] use the thread pool to implement a simple thread pool
Primary school, session 3 - afternoon: Web_ sessionlfi
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Summary of operating system interview questions (updated from time to time)
【ICLR 2021】半监督目标检测:Unbiased Teacher For Semi-Supervised Object Detection
Advanced skills of testers: a guide to the application of unit test reports
WeakSet
腾讯会议应用市场正式上线,首批入驻超20款应用








