hI I use these functions for hashing ..
Code:
int hash(char *key)
{
unsigned int hash_val = 0;
while(*key != '\0')
hash_val += *(key++);
return(hash_val % HASHSIZE);
}
int hash_insert (char *key,int key_value)
{
listNode *newptr,*curr,*ptr;
int pos;
pos = hash(key);
/* make sure it's not already in the hash table */
curr = HashTable[pos];
while(curr != NULL) {
if (!strcmp(curr->code_name, key))
{curr->total_points=curr->total_points+key_value;
}
curr = curr->next;
}
ptr = HashTable[pos];
newptr = (struct listNode *)malloc(sizeof(listNode));
newptr->code_name = (char *)calloc( strlen( key) + 1, sizeof( char ) );
strcpy(newptr->code_name,key); /*or newptr>code_name=key;*/
newptr->total_points=key_value;
newptr->next = NULL;
if (HashTable[pos] == NULL)
{
HashTable[pos] = newptr;
}
else
{
while(ptr != NULL)
{
ptr = ptr->next;
}
ptr->next = newptr;
}
return 1;
}
char* hash_find(char* key)
{
int pos;
pos = hash(key);
while(HashTable[pos] != NULL) {
if (!strcmp(HashTable[pos]->code_name, key))
{ printf("\nCodename:%s\nTotal Points:%d\n",HashTable[pos]->code_name ,HashTable[pos]->total_points);
return key;}
else{
HashTable[pos] = HashTable[pos]->next;}
}
return NULL;
}
The probles is that when I store the data from a file i get a error message from windows . any help?