/* 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语言实现实例的更多相关文章

  1. 解决hash冲突之分离链接法

    解决hash冲突之分离链接法 分离链接法:其做法就是将散列到同一个值的所有元素保存到一个表中. 这样讲可能比较抽象,下面看一个图就会很清楚,图如下 相应的实现可以用分离链接散列表来实现(其实就是一个l ...

  2. Python数据结构——散列表

    散列表的实现常常叫做散列(hashing).散列仅支持INSERT,SEARCH和DELETE操作,都是在常数平均时间执行的.需要元素间任何排序信息的操作将不会得到有效的支持. 散列表是普通数组概念的 ...

  3. 分离链接法(Separate Chaining)

    之前我们说过,对于需要动态维护的散列表 冲突是不可避免的,无论你的散列函数设计的有多么精妙.因此我们解决的重要问题就是:一旦发生冲突,我们该如何加以排解? 我们在这里讨论最常见的两种方法:分离链接法和 ...

  4. Python与数据结构[4] -> 散列表[1] -> 分离链接法的 Python 实现

    分离链接法 / Separate Chain Hashing 前面完成了一个基本散列表的实现,但是还存在一个问题,当散列表插入元素冲突时,散列表将返回异常,这一问题的解决方式之一为使用链表进行元素的存 ...

  5. 深入浅出数据结构C语言版(14)——散列表

    我们知道,由于二叉树的特性(完美情况下每次比较可以排除一半数据),对其进行查找算是比较快的了,时间复杂度为O(logN).但是,是否存在支持时间复杂度为常数级别的查找的数据结构呢?答案是存在,那就是散 ...

  6. 哈希表(散列表)—Hash表解决地址冲突 C语言实现

    哈希表(Hash table,也叫散列表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.具体的介绍网上有很详 ...

  7. 【阅读笔记:散列表】Javascript任何对象都是一个散列表(hash表)!

    什么是散列表? 散列表是Dictionary(字典)的一种散列表实现方式,字典传送门 一个很常见的应用是使用散列表来表示对象.Javascript语言内部就是使用散列表来表示每个对象.此时,对象的每个 ...

  8. JavaScript数据结构——集合、字典和散列表

    集合.字典和散列表都可以存储不重复的值. 在集合中,我们感兴趣的是每个值本身,并把它当作主要元素.在字典和散列表中,我们用 [键,值] 的形式来存储数据. 集合(Set 类):[值,值]对,是一组由无 ...

  9. 为什么我要放弃javaScript数据结构与算法(第七章)—— 字典和散列表

    本章学习使用字典和散列表来存储唯一值(不重复的值)的数据结构. 集合.字典和散列表可以存储不重复的值.在集合中,我们感兴趣的是每个值本身,并把它作为主要元素.而字典和散列表中都是用 [键,值]的形式来 ...

随机推荐

  1. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

  2. 读书笔记_Effective_C++_条款四十:明智而审慎地使用多重继承

    多重继承是一种比较复杂的继承关系,它意味着如果用户想要使用这个类,那么就要对它的父类也了如指掌,所以在项目中会带来可读性的问题,一般我们都会尽量选择用单继承去替代它. 使用多重继承过程容易碰到的问题就 ...

  3. 通过yum来安装vsftpd

    Linux系统:centos6.6.  安装步骤  1.通过yum来安装vsftpd [root@localhost ~]# yum -y install vsftpd 2.设置为开机启动 [root ...

  4. Make a DAC with a microcontroller's PWM timer

    http://www.edn.com/design/analog/4337128/Make-a-DAC-with-a-microcontroller-s-PWM-timer Many embedded ...

  5. PHP-系统流程

    我们来系统的了解下ThinkPHP框架开发的应用的标准执行流程: 用户URL请求 调用应用入口文件(通常是网站的index.php) 载入框架入口文件(ThinkPHP.php) 记录初始运行时间和内 ...

  6. CLR基础,CLR运行过程,使用dos命令创建、编译、运行C#文件,查看IL代码

    CLR是Common Language Runtime的缩写,是.NET程序集或可执行程序运行的一个虚拟环境.CLR用于管理托管代码,但是它本身是由非托管代码编写的,并不是一个包含了托管代码的程序集, ...

  7. CMMI5级——原因分析及解决方案(Causal Analysis and Resolution)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010825142/article/details/15338085 聪明的人在出现问题的时候,除了 ...

  8. Android布局优化之ViewStub、include、merge使用与源码分析

    在开发中UI布局是我们都会遇到的问题,随着UI越来越多,布局的重复性.复杂度也会随之增长.Android官方给了几个优化的方法,但是网络上的资料基本上都是对官方资料的翻译,这些资料都特别的简单,经常会 ...

  9. 借助 Resharper 和 StyleCop 让代码更整洁

    一:工具安装 Resharper 和 StyleCop 必须安装. Resharper 的配置文件如下:Resharper.zip 请按如下步骤导入, 1: 2: 3:   StyleCope 的配置 ...

  10. 【BZOJ】【2750】【HAOI2012】Road

    最短路+拓扑序DP orz zyf & lyd 统计每条边在多少条最短路径上……其实可以统计 有多少条最短路径经过了x,以及y出发到达任意一个结束点有多少种走法(沿最短路) 我们可以用Dijk ...