I have a server to receive request from multiple client. I have done this with threads. I want to insert some user name and password to hash table. For that I am using double hash method. It was inserted successfully. But I want to know when user enter a user name, I need to search on hash table whether this username already present. But I can't do it now. I know the concept like using hashing. Get the index using hashfunction1 through username and use double hash like this. But how can I write that code ?
My code:
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
}
How can I write code to search a username is already present using double hashing from hash table in C? I think traverse whole hash table is not good practice ..is it?