哈希表
来源:互联网 发布:oracle数据库漏洞 编辑:程序博客网 时间:2024/05/21 16:59
工程中的代码实现常常会需要键-值的映射
- 即给出一个值即可得到其代表的数据
哈希的定义:
- 在数据元素的存储位置和它的关键字之间建立一个映射关系f,通过f可以直接得到关键字所代表的数据元素
哈希表:
- 哈希技术中用于存储数据元素的数据结构
哈希函数:
- 哈希技术中的映射关系f
数组时最简单也是最高效的哈希实现
哈希技术需要具体的数据结构为基础
常用操作:
- 创建哈希
- 销毁哈希
- 清空哈希
- 加入键值对
- 删除键值对
- 根据键获取值
- 获取键值对数目
Hash.h
#ifndef HASH_H#define HASH_Htypedef void Hash;typedef void HashKey;typedef void HashValue;typedef int (Hash_Compare)(Hashkey* ,HashKey*);Hash* Hash_Create();void Hash_Destroy(Hash* hash);void Hash_Clear(Hash* hash);int Hash_Add(Hash* hash,HashKey* key,HashValue* value,Hash_Compare* compare);HashValue* Hash_Remove(Hash* hash,HashKey* key,Hash_Compare* compare);HashValue* Hash_Get(Hash* hash,HashKey* key,Hash_Compare* compare);int Hash_Count(Hash* hash);#endif
Hash.c
#include "Hash.h"#include "BSTree.h"typedef struct tag_HashNode HashNode;void recursive_clear(BSTreeNode* node){ if(node != NULL) { recursive_clear(node->left); recursive_clear(node->right); free(node); } }struct tag_HashNode{ BSTreeNode header; HashValue* value;};Hash* Hash_Create(){ return BSTree_Create();}void Hash_Destroy(Hash* hash){ Hash_Clear(hash); BSTree_Destroy(hash);}void Hash_Clear(Hash* hash){ recursive_clear(hash); BSTree_Clear(hash);}int Hash_Add(Hash* hash,HashKey* key,HashValue* value,Hash_Compare* compare){ int ret=0; HashNode* node = (HashNode*)malloc(sizeof(HashNode)); if(node != NULL) { node->header.key = key; node->value = value; ret = BSTree_Insert(hash,(BSTreeNode*)node,compare); if(!ret) { free(node); } } return ret;}HashValue* Hash_Remove(Hash* hash,HashKey* key){ HashValue* ret = NULL; HashNode* node = (HashNode*)BSTree_Delete(hash,key,compare); if(node != NULL) { ret = node->value; free(node); } return ret;}HashValue* Hash_Get(Hash* hash,HashKey* key){ HashValue* ret = NULL; HashNode* node = (HashNode*)BSTree_Get(hash,key,compare); if(node != NULL) { ret = node->value; } return ret;}int Hash_Count(Hash* hash){ return BSTree_Count(hash);}
阅读全文
0 0
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- .哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- 哈希表
- Mysql高级教程-索引-04
- 前端开发如何实现结构、样式、行为彻底分离
- 简单聊聊SOA和微服务
- 77 C语言函数的声明以及函数原型
- 复习-高等数学的求导
- 哈希表
- 【IO流】FileInputStream,FileOutputStream读写文件
- js的运算符
- 【opencv3.0】鱼眼图像畸变校正——标定校正
- Golang -- goroutine实例(乒乓球双打比赛)
- 链栈初始化、进栈、出栈、判空、遍历、求长、求顶、清栈、毁栈
- Magic Number LCA求路径点集的并
- Flask在linux系统下环境的离线搭建及简单实用教程
- 树莓派下安装Django环境