当前位置:网站首页>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
边栏推荐
- 永远不要使用Redis过期监听实现定时任务!
- Go language learning tutorial (10)
- await和async
- pycharm有用快捷键
- DELL R720服务器安装网卡Broadcom 5720驱动
- 软件工程最佳实践——项目需求分析
- Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
- Growth summer challenge is coming, exclusive community welfare is coming ~ get CSDN customized T-shirt for free
- Friends in Guangzhou can join us if they have the opportunity
- Playwright - 滚动条操作
猜你喜欢

ABAQUS 2022最新版——完善的现实仿真解决方案

腾讯会议应用市场正式上线,首批入驻超20款应用

测试人进阶技能:单元测试报告应用指南

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Baseline

盘点华为云GaussDB(for Redis)六大秒级能力

Why must a digital transformation strategy include continuous testing?

1. 爬虫之Beautifulsoup解析库&在线解析图片验证码

科大讯飞活跃竞赛汇总!(12个)

Abaqus 2022软件安装包和安装教程
随机推荐
composer
计网 | 【五 传输层、六 应用层】知识点及例题
Why do more and more people choose cloud rendering?
Convert seconds to * * hours * * minutes
pycharm从安装到全副武装,学起来才嗖嗖的快,图片超多,因为过度详细!
SSM整合流程(整合配置、功能模块开发、接口测试)
VR全景中特效是如何编辑的?细节功能如何展示?
arthas调试 确定问题工具包
小学期,第三场-下午:WEB_xxe
Wechat applets - basics takes you to understand the life cycle of applets (2)
测试必备工具 —— Postman实战教程
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
matlab Delaunay 三角剖分内的查询点
盘点华为云GaussDB(for Redis)六大秒级能力
DNS服务器搭建、转发、主从配置
标配10个安全气囊,奇瑞艾瑞泽8安全防护无死角
SQL continuous login problem
【已解决】抖音如何取消关注已注销的账户
事件队列、微任务与宏任务的理解和面试题
笔记软件的历史、选择策略以及深度评测