散列的C语言实现
int hashval = func(key,hashmap->
while(hashmap->
if((hashmap->
&& (strcmp(hashmap->map[hashval].pair->
hashval %= hashmap->
if(hashmap->
hashmap->
hashmap->
hashmap->
else if((hashmap->
&& (strcmp(hashmap->map[hashval].pair->
free(hashmap->map[hashval].pair->
hashmap->map[hashval].pair->
&& (strcmp(map[hashval].pair->
(strcmp(map[hashval].pair->
free(map[hashval].pair->
map[hashval].pair->
map[hashval].pair->
int size = next_prime(2*hashmap->
if(hashmap->
hashmap->map[i].pair->
hashmap->map[i].pair->
delete_array(&hashmap->map,hashmap->
hashmap->
hashmap->
if(hashmap->
//hashmap->
//hashmap->
int hashval = func(key, hashmap->
while(hashmap->
if((hashmap->
&& (strcmp(hashmap->map[hashval].pair->
hashval %= hashmap->
if((hashmap->
&& (strcmp(hashmap->map[hashval].pair->
return hashmap->
int hashval = func(key, hashmap->= func(key, hashmap->capacity);
int index = 0;
/**********************************************
*退出循环的条件是找到空闲的单元,或者关键字匹配
*********************************************/
while(hashmap->map[hashval].state != EMPTY)
{
if((hashmap->map[hashval].state == ACTIVE)
&& strcmp(hashmap->map[hashval].pair->key,key) == 0)
break;
index ++;
hashval += index * index;
hashval %= hashmap->capacity;
if(index == 200)
break;
}
/*判断删除条件*/
if((hashmap->map[hashval].state == ACTIVE) &&
(strcmp(hashmap->map[hashval].pair->key, key) == 0))
{
hashmap->map[hashval].state = DELETED;
delete_pair(&(hashmap->map[hashval].pair));
hashmap->map[hashval].pair = NULL;
return true;
}
return false;
}
bool delete_hashnode(Hashmap_handle_t hashmap, const char *key)
{
if(delete_node(hashmap, key, HashFunc))
{
hashmap->size --;
return true;
}
return false;
}
参数获取函数;
int Length(Hashmap_t *map)
{
return map->size;
}
int Capacity(Hashmap_t *map)
{
return map->capacity;
}
void delete_hashmap(Hashmap_handle_t hashmap)
{
int i = 0;
int size = hashmap->capacity;
if(hashmap != NULL)
{
for(; i < size; ++ i)
{
if(hashmap->map[i].state == ACTIVE)
{
delete_pair(&(hashmap->map[i].pair));
hashmap->map[i].state = DELETED;
hashmap->map[i].pair = NULL;
hashmap->size --;
}
}
free(hashmap->map);
hashmap->map = NULL;
}
}
void free_hashmap(Hashmap_handle_t *hashmap)
{
delete_hashmap(*hashmap);
free(*hashmap);
*hashmap = NULL;
}
char *key_pair(Pair_handle_t pair)
{
if(pair != NULL)
{
return pair->key;
}
return NULL;
}
char *value_pair(Pair_handle_t pair)
{
if(pair != NULL)
{
return pair->value;
}
return NULL;
}
/*复制散列操作,相当于创建了一个新的散列对象,而不是指针复制*/
Hashmap_handle_t copy_hashmap(Hashmap_handle_t hashmap)
{
Hashmap_handle_t newhashmap = NULL;
int i = 0;
if(hashmap == NULL)
return NULL;
/*采用动态分配的方式实现*/
if(!alloc_hashmap(&newhashmap,hashmap->capacity))
return NULL;
for(; i < hashmap->capacity ; ++ i)
{
if(hashmap->map[i].state == ACTIVE)
{
/*得到当前中的值实现插入操作*/
insert_hashnode(newhashmap,
hashmap->map[i].pair->key,
hashmap->map[i].pair->value);
}
else if(hashmap->map[i].state == DELETED)
{
newhashmap->map[i].state = DELETED;
}
}
return newhashmap;
}
测试函数:
#include "hashmap.h"
#include #define CAPACITY 13 char *keys[] = { "abcd", "defh", "abcd", "12345", "a1b2c3", "12345", "a1b2c3", "23456", "hijhk", "test1", "test1", "789123", "Input", }; char *values[] = { "abcd", "defh", "abcd", "12345", "a1b2c3", "12345", "a1b2c3", "23456", "hijhk", "test1", "test1", "789123", "Input", }; int myhashFunc(const char *key, int Tabsize) { int hashVal = 0; int i = 0; int len = strlen(key); for(; i < len ; ++ i ) hashVal += 37 *hashVal + key[i]; hashVal %= Tabsize; if(hashVal < 0) hashVal += Tabsize; return hashVal; } int main() { int i = 0; char str1[13]; char str2[13]; Hashmap_t mymap ; Hashmap_handle_t map = NULL; Hashmap_handle_t doubmap = NULL; Pair_handle_t pair; /*静态分配*/ srand((int)time(0)); printf("init and alloc test:"); if(!init_hashmap(&mymap,13)) { ret
散列C语言数组存储方 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)