当前位置:网站首页>C语言:hashTable
C语言:hashTable
2022-06-30 19:20:00 【B!GGer.】
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*Func)(int, int);
//拉链法
//链表节点,数据域存字符串
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;
}
//顺序表(表头,大小)
typedef struct HashTable{
//链表首地址,是链表头结点的顺序表,由于头结点是Node *,顺序表示Node **
Node **data;
int size;
}HashTable;
//初始化链表,头插法,产生的新节点的next = 传入节点
Node *init_node(Func func, Node *head){
Node *p = (Node *)malloc(sizeof(Node));
p->func = func;
p->next = head;
return p;//虚拟头结点
}
HashTable *init_hashtable(int n){
HashTable *h = (HashTable *)malloc(sizeof(HashTable));
//h->size = n << 1;//若要使哈希表存储效率变高,存储空间扩大一倍??
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];
//保证正数
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;
}
参考链接:https://blog.csdn.net/one_chow_chow/article/details/109561575
边栏推荐
- ABAQUS 2022最新版——完善的现实仿真解决方案
- Simple usage of LinkedList (2022.6.13-6.19)
- 英语没学好到底能不能做coder,别再纠结了先学起来
- 当我们在看待产业互联网的时候,总是会站在消费互联网的对立面来看待它
- PS2手柄-1「建议收藏」
- MQ advantages and disadvantages (2022.5.2-5.8)
- What securities dealers recommend? In addition, is it safe to open a mobile account?
- Lombok
- 闲鱼难“翻身”
- Growth summer challenge is coming, exclusive community welfare is coming ~ get CSDN customized T-shirt for free
猜你喜欢

线下门店为什么要做新零售?

说实话ThreadLocal真不是啥高级的东西

4.3寸触控屏12路控制端口可编程网络中控支持5台中控主机相互备份

Why do more and more people choose cloud rendering?

超视频时代的音视频架构建设|Science和英特尔联袂推出“架构师成长计划”第二季

VoIP Push 在海外音视频业务中的应用

Force deduction ----- count the string containing the given prefix

【LeetCode】【SQL】刷题笔记

FH6908A负极关断同步整流模拟低压降二极管控制IC芯片TSOT23-6超低功耗整流器 1w功耗 <100uA静态 替代MP6908

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
随机推荐
4.3-inch touch screen 12 channel control port programmable network central control supports mutual backup of 5 central control hosts
FH6908A负极关断同步整流模拟低压降二极管控制IC芯片TSOT23-6超低功耗整流器 1w功耗 <100uA静态 替代MP6908
Which brokerage has the lowest commission? In addition, is it safe to open a mobile account?
请指教在线开户是什么意思?究竟网上开户是否安全么?
The project is configured with eslint. When the editor does not close the eslint function, the eslint does not take effect
qt中toLocal8Bit和toUtf8()有什么区别
History, selection strategy and in-depth evaluation of note taking software
漫画 | Oracle 被新时代抛弃了吗?
SQL continuous login problem
WeakSet
Whether the SQL is indexed
【450. 删除二叉搜索树中的节点】
Smarter! Airiot accelerates the upgrading of energy conservation and emission reduction in the coal industry
matlab 将三角剖分结果保存为STL文件
《微信小程序-基础篇》带你了解小程序中的生命周期(二)
composer
VR云展厅如何给线下实体带来活力?有哪些功能?
Alibaba Tianchi SQL training camp learning notes 5
Go language learning tutorial (10)
达梦数据库重新初始化实例操作记录