分离链接散列表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数据结构与算法(第七章)—— 字典和散列表
本章学习使用字典和散列表来存储唯一值(不重复的值)的数据结构. 集合.字典和散列表可以存储不重复的值.在集合中,我们感兴趣的是每个值本身,并把它作为主要元素.而字典和散列表中都是用 [键,值]的形式来 ...
随机推荐
- 【原】MySQL实用SQL积累
[文档简述] 本文档用来记录一些常用的SQL语句,以达到快速查询的目的. [常用SQL] 1.mysql数据库中获取某个表的所有字段名 select COLUMN_NAME from informat ...
- 字符串转码【String.getBytes()和new String()】
在Java中,String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如 byte[] b_gbk = "中&q ...
- js的栈与堆
JavaScript中基本数据类型和引用数据类型的区别 这是我引用别人的 觉得很好 1.基本数据类型和引用数据类型 ECMAScript包括两个不同类型的值:基本数据类型和引用数据类型. 基本 ...
- flash从数据流判断图片格式防止xss攻击
前段时间测试人员报了一个flash的xss bug,经分析用了Loader.loadBytes且没做数据流格式校验的程序都会中招,自测方法只需一行代码: ExternalInterface.call( ...
- redhat 6.6 安装 (LVM)
http://www.cnblogs.com/kerrycode/p/4341960.html
- VS中运行HTTP 无法注册URL
参考资料 http://www.java123.net/detail/view-449670.html http://www.cnblogs.com/jiewei915/archive/2010/06 ...
- 报错:不允许保存更改。您所做的更改要求删除并重新创建以下表……
在使用SQL Server 2008为某个表添加列的时候出现启用了"阻止保存要求重新创建表的更改问题的设置方法..."报错: 解决方法: 工具--选项--Designers-- ...
- 25LINQ拾遗及实例
投影 □ 遍历数组索引,Select获取 int[] indexes = {0, 2}; string[] strs = {"a", "b", " ...
- Android项目实战之(1)-- 开发一个"快速冲浪"的程序
概述:这个小程序,你讲学习到基本控件(Button,Listview,Gridview,TextView等)的使用技巧,AssetManager类的使用,XML数据的解析方式,BaseAdapter, ...
- springboot2.X集成HttpClient 发送HTTPS 请求
1)jar <!--httpclient 发送外部https/http 请求--> <dependency> <groupId>org.apache.httpcom ...