分离链接散列表C语言实现实例
/* hash_sep.h */ #ifndef _HASH_SEP_H
#define _HASH_SEP_H #define MIN_TABLE_SIZE 5
struct list_node;
typedef struct list_node *position;
struct hash_tbl;
typedef struct hash_tbl *hash_table;
typedef unsigned int hash_index; hash_index hash(const char *key, int table_size);
hash_table initialize_table(int table_size);
void destroy_table(hash_table h);
position find(char *key, hash_table h);
void insert(char *key, hash_table h);
char *retrieve(position p); #endif
/* hash_sep.c */ #include "hash_sep.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h> struct list_node
{
char element[100];
position next;
}; typedef position list; /* List *the_lists will be an array of lists, allocated later */
/* the lists use headers (for simplicity), */
/* though this wastes space */
struct hash_tbl
{
int table_size;
list *the_lists;
}; hash_index
hash(const char *key, int table_size)
{
unsigned int hash_val = 0; while(*key != '\0')
hash_val = (hash_val << 5) + *key++; return hash_val % table_size;
} hash_table
initialize_table(int table_size)
{
hash_table h;
int i; if(table_size < MIN_TABLE_SIZE)
{
printf("Table size too small");
return NULL;
} /* allocate table */
h = malloc(sizeof(struct hash_tbl));
if(h == NULL)
{
perror("malloc");
exit(1);
} h->table_size = table_size; /* allocate array of lists */
h->the_lists = malloc(sizeof(list) * h->table_size);
if(h->the_lists == NULL)
{
perror("malloc");
exit(1);
} /* allocate list headers */
for(i = 0; i < h->table_size; i++)
{
h->the_lists[i] = malloc(sizeof(struct list_node));
if(h->the_lists[i] == NULL)
{
perror("malloc");
exit(1);
}
else
h->the_lists[i]->next = NULL;
} return h;
} position
find(char *key, hash_table h)
{ position p;
list l; l = h->the_lists[hash(key, h->table_size)];
p = l->next; while(p != NULL && strcmp(p->element, key))
p = p->next; return p;
} void
insert(char *key, hash_table h)
{
position pos;
position new_cell;
list l; pos = find(key, h);
if(pos == NULL)
{
new_cell = malloc(sizeof(struct list_node));
if(new_cell == NULL)
{
perror("malloc");
exit(1);
}
else
{
l = h->the_lists[hash(key, h->table_size)];
new_cell->next = l->next;
strcpy(new_cell->element, key);
l->next = new_cell;
}
}
} void
destroy_table(hash_table h)
{
int i; if(h != NULL)
{
for(i = 0; i < h->table_size; i++)
free(h->the_lists[i]);
free(h->the_lists);
free(h);
}
} char *
retrieve(position p)
{
return p->element;
}
/* hash_test.c */ #include <stdio.h>
#include "hash_sep.h" int
main(void)
{
position p1;
position p2;
position p3;
char *tmp1;
char *tmp2;
char *tmp3;
char array1[] = "zhu";
char array2[] = "yong";
char array3[] = "chang";
hash_table htp; htp = initialize_table(10); insert(array1, htp);
insert(array2, htp);
insert(array3, htp);
p1 = find("zhu", htp);
p2 = find("yong", htp);
p3 = find("chang", htp);
tmp1 = retrieve(p1);
tmp2 = retrieve(p2);
tmp3 = retrieve(p3); printf("\n%s %s %s\n\n", tmp1, tmp2, tmp3); destroy_table(htp);
}
实例测试结果:

分离链接散列表C语言实现实例的更多相关文章
- 解决hash冲突之分离链接法
解决hash冲突之分离链接法 分离链接法:其做法就是将散列到同一个值的所有元素保存到一个表中. 这样讲可能比较抽象,下面看一个图就会很清楚,图如下 相应的实现可以用分离链接散列表来实现(其实就是一个l ...
- Python数据结构——散列表
散列表的实现常常叫做散列(hashing).散列仅支持INSERT,SEARCH和DELETE操作,都是在常数平均时间执行的.需要元素间任何排序信息的操作将不会得到有效的支持. 散列表是普通数组概念的 ...
- 分离链接法(Separate Chaining)
之前我们说过,对于需要动态维护的散列表 冲突是不可避免的,无论你的散列函数设计的有多么精妙.因此我们解决的重要问题就是:一旦发生冲突,我们该如何加以排解? 我们在这里讨论最常见的两种方法:分离链接法和 ...
- Python与数据结构[4] -> 散列表[1] -> 分离链接法的 Python 实现
分离链接法 / Separate Chain Hashing 前面完成了一个基本散列表的实现,但是还存在一个问题,当散列表插入元素冲突时,散列表将返回异常,这一问题的解决方式之一为使用链表进行元素的存 ...
- 深入浅出数据结构C语言版(14)——散列表
我们知道,由于二叉树的特性(完美情况下每次比较可以排除一半数据),对其进行查找算是比较快的了,时间复杂度为O(logN).但是,是否存在支持时间复杂度为常数级别的查找的数据结构呢?答案是存在,那就是散 ...
- 哈希表(散列表)—Hash表解决地址冲突 C语言实现
哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...
- 【阅读笔记:散列表】Javascript任何对象都是一个散列表(hash表)!
什么是散列表? 散列表是Dictionary(字典)的一种散列表实现方式,字典传送门 一个很常见的应用是使用散列表来表示对象.Javascript语言内部就是使用散列表来表示每个对象.此时,对象的每个 ...
- JavaScript数据结构——集合、字典和散列表
集合.字典和散列表都可以存储不重复的值. 在集合中,我们感兴趣的是每个值本身,并把它当作主要元素.在字典和散列表中,我们用 [键,值] 的形式来存储数据. 集合(Set 类):[值,值]对,是一组由无 ...
- 为什么我要放弃javaScript数据结构与算法(第七章)—— 字典和散列表
本章学习使用字典和散列表来存储唯一值(不重复的值)的数据结构. 集合.字典和散列表可以存储不重复的值.在集合中,我们感兴趣的是每个值本身,并把它作为主要元素.而字典和散列表中都是用 [键,值]的形式来 ...
随机推荐
- CodeForces 128D Numbers 构造
D. Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- wikioi 1380 没有上司的舞会 树形dp
1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他 ...
- 读书笔记_Effective_C++_条款三十一:将文件间的编译依存关系降至最低(第三部分)
下面来谈谈书中的第二部分,用Interface Classes来降低编译的依赖.从上面也可以看出,避免重编的诀窍就是保持头文件(接口)不变化,而保持接口不变化的诀窍就是不在里面声明编译器需要知道大小的 ...
- CareerCup之1.3字符串去重
[题目] 原文: 1.3 Design an algorithm and write code to remove the duplicate characters in a string witho ...
- 《TCP/IP具体解释卷2:实现》笔记--IP:网际协议
本章介绍IP分组的结构和主要的IP处理过程,包含输入,转发和输出. 下图显示了IP层常见的组织形式. 在之前的文章中.我们看到了网络接口怎样把到达的IP分组放到IP输入队列ipintrq中去,并怎样调 ...
- 聊聊JVM的年轻代(转)
聊聊JVM的年轻代 本文转自http://ifeve.com/jvm-yong-generation/ 1.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代 ...
- appium+python自动化53-adb logcat查看日志
前言 做app测试,遇到异常情况,查看日志是必不可少的,日志如何输出到手机sdcard和电脑的目录呢?这就需要用logcat输出日志了 以下操作是基于windows平台的操作:adb logcat | ...
- Android之Volley使用
转自:http://blog.csdn.net/lfdfhl/article/details/12223345 稍微做了一点儿修改 /** * 利用NetworkImageView显示网络图片 */ ...
- from __future__ import print_function
1.在python2.x的环境是使用下面语句,则第二句语法检查通过,第三句语法检查失败 from __future__ import print_function priint('good') pri ...
- Ubuntu Linux自动发邮件配置及邮件发送脚本
测试环境:Ubuntu 11.10 1. 安装mutt及msmtp软件 sudo apt-get install mutt sudo apt-get install msmtp 2. 编辑配置文件vi ...