散列的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->
trlen(value)] = ;
free(hashmap->map[hashval].pair->value);
hashmap->map[hashval].pair->value = newstr;
newstr = NULL;
return true;
}
}
return false;
}
static bool insert2map(HashEntry_handle_t map,
const char *key, const char *value,
hashfunc func, int size)
{
int hashval = func(key,size);
int index = 0;
char *newstr = NULL;
Pair_handle_t newpair = NULL;
if(map != NULL)
{
while(map[hashval].state != EMPTY)
{
if((map[hashval].state == ACTIVE)
&& (strcmp(map[hashval].pair->key, key) == 0))
break;
index ++;
hashval += index * index;
hashval %= size;
/*防止死循环*/
if(index == 200)
break;
}
if(map[hashval].state == EMPTY)
{
if(!make_pair(&newpair,key,value))
return false;
map[hashval].pair = newpair;
map[hashval].state = ACTIVE;
newpair = NULL;
return true;
}
else if((map[hashval].state == ACTIVE) &&
(strcmp(map[hashval].pair->key, key) == 0))
{
newstr = (char *)malloc(strlen(value) +1);
if(newstr != NULL)
{
free(map[hashval].pair->value);
map[hashval].pair->value = NULL;
strcpy(newstr, value);
newstr[strlen(value)] = ;
map[hashval].pair->value = newstr;
return true;
}
}
}
return false;
}
调整大小和插入的实际操作。
bool resize(Hashmap_handle_t hashmap)
{
int i = 0;
HashEntry_handle_t newarray = NULL;
int size = next_prime(2*hashmap->capacity);
if(hashmap != NULL)
{
newarray = (HashEntry_handle_t)malloc(sizeof(HashEntry_t)
*size);
if(newarray == NULL)
return false;
/*这一步至关重要*/
Tabinital(newarray, size);
for(; i < hashmap->capacity ; ++ i)
{
if(hashmap->map[i].state == ACTIVE)
{
if(!insert2map(newarray,
hashmap->map[i].pair->key,
hashmap->map[i].pair->value,
HashFunc, size))
return false;
}
}
delete_array(&hashmap->map,hashmap->capacity);
hashmap->map = newarray;
hashmap->capacity = size;
return true;
}
return false;
}
bool insert_hashnode(Hashmap_handle_t hashmap,
const char *key, const char *value)
{
if(hashmap->size < hashmap->capacity)
{
if(insert_data(hashmap,key,value,HashFunc))
{
//hashmap->size ++;
}
return true;
}
else /*增加容量*/
{
if(!resize(hashmap))
return false;
if(insert_data(hashmap,key,value,HashFunc))
{
//hashmap->size ++;
}
return true;
}
return false;
}
搜索操作
static Pair_handle_t search_data(Hashmap_handle_t hashmap, const char *key, hashfunc func)
{
int hashval = 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))
{
return hashmap->map[hashval].pair;
}
return NULL;
}
Pair_handle_t search_hashnode(Hashmap_handle_t hashmap, const char * key)
{
return search_data(hashmap,key,HashFunc);
}
删除操作
static bool delete_node(Hashmap_handle_t hashmap,const char *key,hashfunc func)
{
int hashval = func(key, hashmap-> int hashval
散列C语言数组存储方 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)