分离链接法解决hash冲突问题(C语言实现)
2016-08-08 09:30
369 查看
/* hashsep.c */
#include
#include
#include "hashsep.h"
#include "fatal.h"
#define MIN_TABLE_SIZE (10)
struct list_node
{
element_type element;
pos next;
};
typedef pos list;
/* 为了简单起见使用了带头节点的链表 */
struct hash_tbl
{
int table_size;
list *lists;
};
static int
next_prime( int N )
{
int i;
if ( N % 2 == 0 )
N++;
for ( ; ; N += 2 ){
for ( i = 3; i * i <= N; i += 2 ){
if ( N % i == 0 )
goto cont_outer;
}
return N;
cont_outer: ;
}
}
/* 采用简单的取模方法 */
static Index
hash ( element_type key, int table_size )
{
return key % table_size;
}
static hash_table
initialize_talbe( int table_size )
{
hash_table h;
int i;
if ( table_size < MIN_TABLE_SIZE ){
Error("Table size too small!");
return NULL;
}
h = malloc( sizeof( struct hash_tbl ) );
if ( h == NULL )
Error("Out of space!");
h->table_size = next_prime(table_size);
h->lists = malloc( sizeof( list ) * h->table_size );
if ( h->lists == NULL ){
free(h);
Error("Out of space!");
}
/* 预先分配全部头节点 */
for ( i = 0; i < h->table_size; i++){
h->lists[i] = malloc( sizeof( struct list_node ) );
if ( h->lists[i] == NULL ){
Error("Out of space!");
goto init_fail;
}else{
h->lists[i]->next = NULL;
}
}
return h;
init_fail:
for ( i = 0; i < h->table_size || h->lists[i] == NULL; i++){
free( h->lists[i] );
h->lists[i] = NULL;
}
free( h->lists );
free( h );
return NULL;
}
static pos
find( element_type key, hash_table h )
{
pos p;
list l;
l = h->lists[ hash( key, h->table_size ) ];
p = l->next;
while ( p != NULL && p->element != key )
p = p->next;
return p;
}
static void
insert( element_type key, hash_table h )
{
pos p, new_cell;
list l;
p = find( key, h );
if ( p == NULL ){
new_cell = malloc( sizeof( struct list_node ) );
if ( new_cell == NULL )
Error("Out of space!");
else{
l = h->lists[ hash( key, h->table_size ) ];
new_cell->next = l->next;
new_cell->element = key;
l->next = new_cell;
}
}
}
static element_type
retrieve( pos p)
{
return p->element;
}
static void
destroy_table( hash_table h )
{
int i;
pos p, tmp;
for ( i = 0; i < h->table_size; i++ ){
p = h->lists[i];
while ( p != NULL ){
tmp = p->next;
free( p );
p = tmp;
}
}
free( h->lists );
free( h );
}
/************ The next is test functions ***************/
#define get_array_size(array) ( sizeof(array) / sizeof(array[0]) )
void
test_hashsep( void )
{
int i;
pos p;
hash_table h;
element_type datas[100];
for ( i = 0; i < get_array_size( datas ); i++ ){
datas[i] = i;
}
h = initialize_talbe( 13 );
if ( h == NULL )
Error("Initialize error!");
for ( i = 0; i < get_array_size( datas ); i++ ){
insert( datas[i], h );
}
p = find( 31, h );
p = find( 65, h );
return;
}
/***************************************************************************/
/* hashsep.h */
#ifndef _HASHSEP_H_
#define _HASHSEP_H_
typedef int element_type;
typedef int Index;
struct list_node;
typedef struct list_node *pos;
struct hash_tbl;
typedef struct hash_tbl *hash_table;
#endif
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1315) | 评论(0) | 转发(0) |
0
上一篇:二叉堆--C语言实现
下一篇:给skyeye上跑的uClinux挂上gdb o(∩_∩)o(转自新浪blog)
相关热门文章
Python 包管理工具解惑
app开发费用
APP开发流程,你知道多少...
app开发需要多少钱
iredmail迁移纪实
test123
编写安全代码——小心有符号数...
使用openssl api进行加密解密...
一段自己打印自己的c程序...
sql relay的c++接口
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
#include
#include
#include "hashsep.h"
#include "fatal.h"
#define MIN_TABLE_SIZE (10)
struct list_node
{
element_type element;
pos next;
};
typedef pos list;
/* 为了简单起见使用了带头节点的链表 */
struct hash_tbl
{
int table_size;
list *lists;
};
static int
next_prime( int N )
{
int i;
if ( N % 2 == 0 )
N++;
for ( ; ; N += 2 ){
for ( i = 3; i * i <= N; i += 2 ){
if ( N % i == 0 )
goto cont_outer;
}
return N;
cont_outer: ;
}
}
/* 采用简单的取模方法 */
static Index
hash ( element_type key, int table_size )
{
return key % table_size;
}
static hash_table
initialize_talbe( int table_size )
{
hash_table h;
int i;
if ( table_size < MIN_TABLE_SIZE ){
Error("Table size too small!");
return NULL;
}
h = malloc( sizeof( struct hash_tbl ) );
if ( h == NULL )
Error("Out of space!");
h->table_size = next_prime(table_size);
h->lists = malloc( sizeof( list ) * h->table_size );
if ( h->lists == NULL ){
free(h);
Error("Out of space!");
}
/* 预先分配全部头节点 */
for ( i = 0; i < h->table_size; i++){
h->lists[i] = malloc( sizeof( struct list_node ) );
if ( h->lists[i] == NULL ){
Error("Out of space!");
goto init_fail;
}else{
h->lists[i]->next = NULL;
}
}
return h;
init_fail:
for ( i = 0; i < h->table_size || h->lists[i] == NULL; i++){
free( h->lists[i] );
h->lists[i] = NULL;
}
free( h->lists );
free( h );
return NULL;
}
static pos
find( element_type key, hash_table h )
{
pos p;
list l;
l = h->lists[ hash( key, h->table_size ) ];
p = l->next;
while ( p != NULL && p->element != key )
p = p->next;
return p;
}
static void
insert( element_type key, hash_table h )
{
pos p, new_cell;
list l;
p = find( key, h );
if ( p == NULL ){
new_cell = malloc( sizeof( struct list_node ) );
if ( new_cell == NULL )
Error("Out of space!");
else{
l = h->lists[ hash( key, h->table_size ) ];
new_cell->next = l->next;
new_cell->element = key;
l->next = new_cell;
}
}
}
static element_type
retrieve( pos p)
{
return p->element;
}
static void
destroy_table( hash_table h )
{
int i;
pos p, tmp;
for ( i = 0; i < h->table_size; i++ ){
p = h->lists[i];
while ( p != NULL ){
tmp = p->next;
free( p );
p = tmp;
}
}
free( h->lists );
free( h );
}
/************ The next is test functions ***************/
#define get_array_size(array) ( sizeof(array) / sizeof(array[0]) )
void
test_hashsep( void )
{
int i;
pos p;
hash_table h;
element_type datas[100];
for ( i = 0; i < get_array_size( datas ); i++ ){
datas[i] = i;
}
h = initialize_talbe( 13 );
if ( h == NULL )
Error("Initialize error!");
for ( i = 0; i < get_array_size( datas ); i++ ){
insert( datas[i], h );
}
p = find( 31, h );
p = find( 65, h );
return;
}
/***************************************************************************/
/* hashsep.h */
#ifndef _HASHSEP_H_
#define _HASHSEP_H_
typedef int element_type;
typedef int Index;
struct list_node;
typedef struct list_node *pos;
struct hash_tbl;
typedef struct hash_tbl *hash_table;
#endif
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1315) | 评论(0) | 转发(0) |
0
上一篇:二叉堆--C语言实现
下一篇:给skyeye上跑的uClinux挂上gdb o(∩_∩)o(转自新浪blog)
相关热门文章
Python 包管理工具解惑
app开发费用
APP开发流程,你知道多少...
app开发需要多少钱
iredmail迁移纪实
test123
编写安全代码——小心有符号数...
使用openssl api进行加密解密...
一段自己打印自己的c程序...
sql relay的c++接口
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- 开放定址法解决hash冲突问题(C语言实现)
- C语言实现最长递增子序列问题的解决方法
- 应用栈解决迷宫问题的C语言实现
- 散列表的C语言实现-分离链接法
- 数据结构——采用分离链接解决冲突问题的散列表
- vue+springboot前后端分离实现单点登录跨域问题解决方法
- tomcat服务器下使用nginx实现最简单的动静分离--解决css、js不加载的问题
- 用单链表解决约瑟夫问题 C语言实现
- 解决散列表冲突问题-分离链接散列表
- java 解决调用c语言实现简单HelloWorld函数生成.dll动态链接库的32位dll文件无法在64位机上运行
- 数三退一的问题解决(C语言和Java实现)
- tomcat服务器下使用nginx实现最简单的动静分离--解决css、js不加载的问题
- 分离链接散列表--C语言实现
- C语言实现的排列组合问题的通用算法、解决方法
- 分离链接散列表实现文件C语言
- 实习番外篇:解决C语言使用Makefile无法实现更好的持续集成问题
- J2EE工程实现中常见安全问题解决对策
- 一步一步SharePoint 2007之二十一:解决实现注册用户后,自动具备访问网站的权限的问题(3)——创建用户
- 一步一步SharePoint 2007之二十:解决实现注册用户后,自动具备访问网站的权限的问题(2)——配置Role
- 解决鼠标在 flash 链接上不停闪动的问题(web页面中)