【发布时间】:2015-07-20 02:34:41
【问题描述】:
我有一个服务器来接收来自多个客户端的请求。我已经用线程做到了这一点。我想在哈希表中插入一些用户名和密码。为此,我使用双哈希方法。已成功插入。但是我想知道当用户输入用户名时,我需要在哈希表上搜索这个用户名是否已经存在。但我现在做不到。我知道像使用散列这样的概念。通过用户名使用 hashfunction1 获取索引并像这样使用双哈希。但是我怎样才能写出那个代码呢?
我的代码:
int HashFunc1 (char *key,int size)
{
int i = 0,value = 0;
for(i = 0 ; key[i] != '\0'; i++)
{
value += ( key[i] + ( i + 1 ) );
}
return value % size;
}
int HashFunc2 (char *key,int size)
{
int i = 0,value = 0;
for(i = 0; key[i] != '\0'; i++)
{
value += ( key[i] + ( i + 1 ) );
}
return (value * size - 1) % size;
}
int Findindex(char *key,struct HashTable **htable)
{
int hashVal = 0,
stepSize = 0;
hashVal = HashFunc1(key, (*htable)->size);
stepSize= HashFunc2(key, (*htable)->size);
/*to avoid collisions)*/
while ( (*htable)->table[hashVal].username != NULL)
{
/*double hahsing process*/
hashVal = hashVal + stepSize;
hashVal = hashVal % (*htable)->size;
}
return hashVal;
}
int insert_to_hashtable(char *key,char *password,struct HashTable **htable)
{
int pos = 0;
/*find the index to insert new user datas*/
pos = Findindex(key,htable);
//code to insert to coresponding data if this empty
}
如何编写代码以使用 C 中的哈希表中的双重哈希来搜索已经存在的用户名?我认为遍历整个哈希表不是一个好习惯..是吗?
【问题讨论】:
-
当我使用用户名搜索特定索引时,但问题是双重哈希。因为我在最初插入用户名时使用双重哈希方法来查找空缺位置。再次搜索后,我遍历整个哈希表。是吗?
标签: c pointers hash hashtable double-hashing