散列的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->
while(hashmap->
if((hashmap->
&& strcmp(hashmap->map[hashval].pair->
hashval %= hashmap->
if((hashmap->
(strcmp(hashmap->map[hashval].pair->
hashmap->
delete_pair(&(hashmap->
hashmap->
hashmap->
return map->
return map->
int size = hashmap->
if(hashmap->
delete_pair(&(hashmap->
hashmap->
hashmap->
hashmap->
free(hashmap->
hashmap->
return pair->
return pair->
if(!alloc_hashmap(&newhashmap,hashmap->
if(hashmap->
hashmap->map[i].pair->
hashmap->map[i].pair->
else if(hashmap->
newhashmap->
urn false;
}
/*动态分配*/
if(!alloc_hashmap(&map,13))
{
return false;
}
printf("Sucessed!");
/*插入测试*/
printf("insert test:");
for(i = 0; i < CAPACITY + 10; ++ i)
{
sprintf(str1,"%d%d",rand()%10+1,rand()%10+1);
sprintf(str2,"%d%d",rand()%10+1,rand()%10+1);
printf("%s->-f(%s)->%d->%s",str1,str1,
myhashFunc(str1,CAPACITY),str2);
// sprintf(str1,"%s",keys[i]);
// sprintf(str2,"%s",values[i]);
if(!insert_hashnode(&mymap,str1,str2))
{
printf("i = %d, insert to mymap failed", i);
break;
}
if(!insert_hashnode(map,str1,str2))
{
printf("i = %d, insert to map failed", i);
break;
}
}
printf("Sucessed!");
/*查找测试*/
printf("search test:");
if((pair = search_hashnode(&mymap,str1)) != NULL)
{
printf("%s->%s",key_pair(pair),value_pair(pair));
}
if((pair = search_hashnode(map,str1)) != NULL)
{
printf("%s->%s",key_pair(pair),value_pair(pair));
}
printf("Sucessed!");
/*delete*/
printf("delete test:");
if(delete_hashnode(&mymap,str1))
{
printf("Deleted success!!");
}
else
{
printf("Sorry, Failed!!");
}
if(delete_hashnode(map,str1))
{
printf("Deleted success!!");
}
else
{
printf("Sorry, Failed!!");
}
printf("Valid length : %d, Capacity : %d",
Length(&mymap),Capacity(&mymap));
printf("Valid length : %d, Capacity : %d",
Length(map),Capacity(map));
/*改变长度*/
printf("resize test:");
if(resize(&mymap))
printf("Sucessed!");
if(resize(map))
printf("Sucessed!");
/*长度*/
printf("Valid length : %d, Capacity : %d",
Length(&mymap),Capacity(&mymap));
printf("Valid length : %d, Capacity : %d",
Length(map),Capacity(map));
printf("Valid length : %d, Capacity : %d",
Length(&mymap),Capacity(&mymap));
printf("Valid length : %d, Capacity : %d",
Length(map),Capacity(map));
printf("copy test:");
doubmap = copy_hashmap(&mymap);
printf("Valid length : %d, Capacity : %d",
Length(doubmap),Capacity(doubmap));
printf("Sucessed!");
/*释放内存*/
printf("free test:");
delete_hashmap(&mymap);
free_hashmap(&map);
free_hashmap(&doubmap);
printf("Valid length : %d, Capacity : %d",
Length(&mymap),Capacity(&mymap));
printf("Sucessed!");
return 0;
}
测试结果:
[gong@Gong-Computer newversion]$ ./main
init and alloc test:
insert test:
48->-f(48)->4->49
108->-f(108)->5->910
78->-f(78)->1->98
87->-f(87)->12->73
36->-f(36)->3->109
59->-f(59)->4->98
32->-f(32)->12->48
210->-f(210)->10->91
105->-f(105)->2->22
41->-f(41)->10->82
1010->-f(1010)->11->69
19->-f(19)->8->64
25->-f(25)->3->45
28->-f(28)->6->104
16->-f(16)->5->83
44->-f(44)->0->86
85->-f(85)->10->72
51->-f(51)->9->27
54->-f(54)->12->57
107->-f(107)->4->210
73->-f(73)->9->27
1010->-f(1010)->11->61
63->-f(63)->10->63
search test:
63->63
63->63
delete test:
Deleted
Deleted
Valid length : 21, Capacity : 29
Valid length : 21, Capacity : 29
resize test:
Valid length : 21, Capacity : 59
Valid length : 21, Capacity : 59
Valid length : 21, Capacity : 59
Valid length : 21, Capacity : 59
copy test:
Valid length : 21, Capacity : 59
free test:
Valid length : 0, Capacity : 59
从实验效果可知,基本上实现了散列的基本操作。
散列C语言数组存储方 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)