I am having problems with loading multiple words with the same hash code. The hash function I chose, from what I can understand, takes the ascii codes of all of the words in the dictionary, and combines them into a number, that it then returns.

unsigned int hash_value = 0;
for (int i = 0, n = strlen(word); i < n; i++)
    hash_value = (hash_value << 2) ^ word[i];
return hash_value % N;

In load, I have tried to make it detect if the hash value for a word has been used before, and to advance the hash table to the "next" pointer if so.

            if (list != NULL)
                int hosh = hash(list->word);
                char *hsh = table[hosh]->word;
                if (hsh != NULL)
                    table[hosh] = table[hosh]->next;
                table[hosh] = list;
                list = list->next;

In this segment of code, the integer hosh is found by hashing each word in the dictionary. Before inserting each word into the hash table array, it checks if the hash table already has a different word for the hash number. If so, the linked list is supposed to advance to its next pointer, before putting the word into the array. If the hash code has never been seen before, the function just ignores the if statement altogether and inserts the word into the table.

The problem I am facing, though, is that the array doesn't advance to next, so when the check function is introduced, it sees the word "caterpillar" as "gummier", as it was most likely overwritten by another word. It sees "a" as "a", with a hash code of 97.

Using printf, I can deduce that the problem originates in the load function. Would it be best to try a different hash function, or is there a different problem that has gone over my head?

Any help would be kindly appreciated, as I am new to programming.

Thanks in advance!

  • I think the problem is that you have forgotten to add a new node thus it is not advancing to the next pointer. – Vsjain Sep 27 '20 at 12:54
  • @vsjain Thank you for clarifying the situation for me, though I am still a little puzzled by where the new node should be. – Devin Atalay Sep 29 '20 at 21:28

You must log in to answer this question.

Browse other questions tagged .