自己写的哈希表以及解决哈希冲突
哈希表就是键值key-value对,使用hash函数让key产生哈希值,当不同的key产生相同的哈希值时就是哈希冲突了,产生哈希冲突可以使用拉链法。
hash.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"static unsigned int table_size[] = {
7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
16381, 32749, 65521, 131071,
262143, 524287, 1048575, 2097151, 4194303, 8388607,
16777211, 33554431, 67108863, 134217727, 268435455,
536870911, 1073741823, 2147483647, 0};/* hash function: return unsignde int */
static unsigned int hash(const char *key)
{unsigned int seed = 131;unsigned int hash = 0;while (*key) {hash = hash * seed + (*key++);}return (hash & 0x7FFFFFFF) % SIZE;
}void hash_insert(struct hash_table *ht, char *key, char *value)
{unsigned int h;struct hash_node *node,*pnode,*fnode;node=pnode=(struct hash_node *)malloc(sizeof(struct hash_node));fnode=(struct hash_node *)malloc(sizeof(struct hash_node));memset(node,0,sizeof(struct hash_node));node->next=NULL;memset(fnode,0,sizeof(struct hash_node));fnode->next=NULL;h = hash(key);if(ht[h].ht==0){ht[h].ht=fnode;}pnode=ht[h].ht;while(pnode->next!=NULL){pnode=pnode->next;}pnode->key=key;pnode->value=value;pnode->next=node;
}char* search(struct hash_table *ht, char *key)
{unsigned int h;struct hash_node *pnode;char *ret=(char*)malloc(sizeof(char));pnode=(struct hash_node *)malloc(sizeof(struct hash_node));h = hash(key);if(ht[h].ht==0){return ret=NULL;}else{pnode=ht[h].ht;while(pnode->next!=NULL&&pnode->key!=key){pnode=pnode->next;}if(pnode->key==key){return ret=pnode->value;}else{return ret=NULL;}}
}int main() {int i;char *a1;char *a2;char *search_key;char *search_ret;struct hash_table *h;struct hash_node *node;h=(struct hash_table *)malloc(sizeof(struct hash_table)*SIZE);memset(h,0,sizeof(struct hash_table)*SIZE);/*for(i=0;i<SIZE;i++){node=(struct hash_node *)malloc(sizeof(struct hash_node));memset(node,0,sizeof(struct hash_node));node->next=NULL;h[i].ht=node;}*/a1="aaa";a2="abc";hash_insert(h,a1,a2);a1="bbb";a2="jkjhk";hash_insert(h,a1,a2);a1="ccc";a2="reew";hash_insert(h,a1,a2);a1="ddd";a2="hyte";hash_insert(h,a1,a2);a1="eee";a2="wwq";hash_insert(h,a1,a2);a1="fff";a2="fd4";hash_insert(h,a1,a2);search_key="dddd";search_ret=search(h,search_key);return 0;
}
另一种方法一开始就for循环初始化node
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"static unsigned int table_size[] = {
7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
16381, 32749, 65521, 131071,
262143, 524287, 1048575, 2097151, 4194303, 8388607,
16777211, 33554431, 67108863, 134217727, 268435455,
536870911, 1073741823, 2147483647, 0};/* hash function: return unsignde int */
static unsigned int hash(const char *key)
{unsigned int seed = 131;unsigned int hash = 0;while (*key) {hash = hash * seed + (*key++);}return (hash & 0x7FFFFFFF) % SIZE;
}hash_insert(struct hash_table *ht, char *key, char *value)
{unsigned int h;struct hash_node *node,*pnode;node=pnode=(struct hash_node *)malloc(sizeof(struct hash_node));memset(node,0,sizeof(struct hash_node));node->next=NULL;h = hash(key);pnode=ht[h].ht;while(pnode->next!=NULL){pnode=pnode->next;}pnode->key=key;pnode->value=value;pnode->next=node;
}int main() {int i;char *a1;char *a2;struct hash_table *h=(struct hash_table *)malloc(sizeof(struct hash_table)*SIZE);struct hash_node *node=NULL;memset(h,0,sizeof(struct hash_table)*SIZE);for(i=0;i<SIZE;i++){node=(struct hash_node *)malloc(sizeof(struct hash_node));memset(node,0,sizeof(struct hash_node));node->next=NULL;h[i].ht=node;}a1="aaa";a2="abc";hash_insert(h,a1,a2);a1="bbb";a2="jkjhk";hash_insert(h,a1,a2);a1="ccc";a2="reew";hash_insert(h,a1,a2);a1="ddd";a2="hyte";hash_insert(h,a1,a2);a1="eee";a2="wwq";hash_insert(h,a1,a2);a1="fff";a2="fd4";hash_insert(h,a1,a2);return 0;
}
hash.h
struct hash_node{struct hash_node* next;char * key;char * value;
};struct hash_table{struct hash_node *ht;
};#define SIZE 1
SIZE就是哈希表数组的大小,现在故意设置其为1,则哈希表退化为普通链表,这里只是为了演示所有的数据应该链接在一起
扩大SIZE为5,因为我们有6对数据,所以必然最少会有2个数据冲突,冲突的放一起:
自己写的哈希表以及解决哈希冲突相关推荐
- 【C++】【哈希表】【哈希函数】实现自己的哈希表,解决哈希冲突;动态哈希表;
文章目录 前言 1.哈希表与哈希函数的引入 2.哈希表 3.哈希表优劣 一.设计 1.一般.通用哈希函数的设计 2.默认哈希函数 二.哈希冲突 1.链地址法.(seperate chaining ) ...
- 哈希表(解决哈希冲突)
哈希表是一种存储记录的连续内存通过哈希函数的应用,通过哈希函数的应用,可以快速存取与查找数据.所谓哈希法(Hashing),就是将本身的键(Key)通过特定的数学函数运算或使用其他的方转化成对应的数据 ...
- 数据结构之哈希表以及常用哈希的算法表达(含全部代码)
目录 为什么要有哈希 哈希表 含义 创建哈希表需要注意的点 算法的选择 哈希冲突的处理 线性探测法 再哈希法 链表法 哈希表的实现(代码部分) 确定结构体(节点) 准备一个哈希算法 创建一个哈希表(即 ...
- 哈希吧,滚雪球学 Python 哈希表与可哈希对象
橡皮擦,一个逗趣的互联网高级网虫,新的系列,让我们一起 Be More Pythonic. 滚雪球学 Python 第二轮 已完成的文章清单 十一.Python 哈希表与可哈希对象 11.1 哈希表( ...
- 数据结构学习笔记 哈希表(一) 哈希表基础与哈希函数
------HR:The first question is what you do if you have a conflict with your manager ? ------You:Hash ...
- 【数据结构】什么是哈希表?为什么哈希表的查询时间复杂度是O(1)?
大家好,我是卷心菜,可以叫我菜菜,大二学生一枚.本篇主要讲解一种数据结构:哈希表.如果您看完文章有所收获,可以三连支持博主哦~,嘻嘻. 文章目录 一.前言 二.数组 三.哈希表 1.百度百科 2.问题 ...
- 【MIT算法导论】哈希表、全域哈希
哈希表及哈希算法 一. 直接寻址 1. 应用条件 当关键字的全域比较小时(也即:候选的关键字的数目较少),直接寻址法才是行之有效的. p.s. 借用下图,即U(全域)很小,那么需要存储的部分K(实际需 ...
- 哈希表——开哈希表和闭哈希表
#include <iostream> using namespace std; #define HashTable_Size 100 //用除留余数法求关键字的哈希地址 int Hash ...
- python建立哈希表_python实现哈希表
复制代码 代码如下: #! /usr/bin/env python #coding=utf-8 #实现哈希表(线性地址再散列) def ChangeKey(key,m,di): key01=(key+ ...
最新文章
- 人工智能行业应用之:为建筑工程提供全新解决方案
- Push Notification (1)Google C2DM 服务
- BOOST_LOCAL_FUNCTION宏用法的测试程序
- java %1$s_%1$s %1$d Android string (java Android 格式化字符串)
- 蒙昧的意思_蒙昧的意思
- python文本解析_如何通过python进行文本解析?
- halcon控制算子Control,持续更新
- 孙鑫-MFC笔记四--文本编程
- openwrt刷回原厂固件_小米路由器4刷breed, pandavan,openwrt
- 九种NPP\GPP数据集介绍和下载
- 服务器pe安装win7系统安装教程,win7 winpe安装过程图文教程
- android usb采集卡,手机USB视频采集卡, 支持IOS、安卓系统
- shopEx数据库错误,无法连接
- unity黄金矿工游戏
- Baltimore System of Classifications of Viruses
- 操作系统学习-1. 操作系统的目标和作用
- nginx 使用详细解
- 《AngularJS深度剖析与最佳实践》一1.4 实现第一个页面:注册
- Getshell总结
- Android界面 Html5还是Native,说说他们的各自的优缺点。
热门文章
- 使用Python,OpenCV,dlib进行睡意检测(疲劳驾驶检测)
- Python监控目录文件夹,并使用SFTP上传目录及文件到linux服务器
- 深蓝学院的深度学习理论与实践课程:第三章
- PCL调错:(3)error C2589“(“:“::“右边的非法标记
- 2021 年第十一届 MathorCup 高校数学建模挑战赛A题分析
- 二、深度学习数据增强方法汇总
- mysql的存储引擎详解_Mysql存储引擎详解
- 剑指offer:面试题28. 对称的二叉树
- 剑指offer:面试题26. 树的子结构
- CUDA+OpenCV实现光线追踪(有无constant)