/* 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. HDU 3161 Iterated Difference 暴力

    Iterated Difference Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. cocos2dx 字符串拼接

    ;i<;i++){ ]; sprintf(str,"%d",i); ]; strcpy(totalFilename, "game_loading") ; ...

  3. HTTP请求头和响应头

      这篇文章简单总结一下HTTP请求头和响应头,并举一些web开发中响应头的用例. 1. HTTP请求头 accept:浏览器通过这个头告诉服务器,它所支持的数据类型.如:text/html, ima ...

  4. WindowsAPI详解——GetDriveType 获得逻辑驱动器的类型

    http://flyxxtt.blogbus.com/logs/43181576.html 函数原型:UINT GetDriveType(LPCTSTR lpRootPathName) 参数lpRoo ...

  5. Lua中调用C函数(lua-5.2.3)

    Lua能够调用C函数的能力将极大的提高Lua的可扩展性和可用性. 对于有些和操作系统相关的功能,或者是对效率要求较高的模块,我们全然能够通过C函数来实现,之后再通过Lua调用指定的C函数. 对于那些可 ...

  6. rac 10g 10.2.0.1升级到10.2.0.5具体解释

        RAC 10.2.0.1 升级到 10.2.0.5 一. 准备: Patch 包:p8202632_10205_LINUX.zip   节点数:3个节点       RAC1    RAC2  ...

  7. 2008 SCI 影响因子(Impact Factor)

    2008 SCI 影响因子(Impact Factor) Excel download 期刊名缩写 影响因子 ISSN号 CA-CANCER J CLIN 74.575 0007-9235 NEW E ...

  8. 为免费app嵌入Admob广告

    为免费app嵌入Admob广告,进而获得广告收入. 1.http://www.admob.com/注册一个帐号, 添加Add Mobile Site/app,输入相关信息后,提交完成, 下载Andro ...

  9. andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用

    package com.example.yanlei.myapplication; import android.media.MediaMetadataRetriever; import androi ...

  10. pytest文档28-重复执行用例(pytest-repeat)

    前言 平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来. 自动化运行用例时候,也会出现偶然的bug,可以针对单个用例, ...