When I test my dictionary manually with the large dictionary, there are no issues. I even get the correct mispelled words. But if I use check50 or even test with smaller dictionaries, it doesn't seem to load (or possibly count) all the words in the dictionary and hence doesnt pass check50.

The behaviour is confusing me. For example, if a dict is less than 12 words, it only loads 1 word. If it is 11 words in the dict, it says 12, Even at about 78 worss, it still says 12. But when I load the large dict it correctly says 143091 wods loaded.

Clearly I have stuffed something up, I just can't figure out what. My best guess is the hash function I found. I grabbed a hash function for the net as a temporary solution because I wanted to try and make my own, but before I muddle with hash functions, I want to make sure the rest of my code is fine.

This is my load function:

bool load(const char *dictionary)

FILE *fp = fopen(dictionary, "r");

 if (fp == NULL)
    printf("Could not open %s.\n", dictionary);
    return 1;

node *head = hashtable[0];   

char word[45];

while (fscanf(fp, "%s", word) != EOF)
    node *new_node = malloc(sizeof(node));
    if (new_node == NULL) 
        return false;
        strcpy(new_node->word, word);

        new_node->next = head;
        head = new_node;

        unsigned long hashed_number = hash_word(word); 
        hashtable[hashed_number] = new_node;           
return true;


I must be doing at least something right, because it works with the large dictionary. Any help would be appreciated.

1 Answer 1


Instead of a variable head, containing arbitrary value hashtable[0], use hashtable[hashed_number] (you might need to do the hashing a bit earlier)

  • Kindly help in this error also: cs50.stackexchange.com/questions/24795/… @Blauelf May 8, 2017 at 10:10
  • head has the value hashtable[0] because it is my root node, that is why i have it pointing at the first element in the array. If head is the issue, I don't understand why my code works with large dictionaries but not small.
    – Matt
    May 8, 2017 at 11:26
  • A hash map has no root node, it contains several linked lists. You create a single linked list, and every entry of the hash map points to various elements of the one linked list. It more or less works by accident, but is not the data structure you intended.
    – Blauelf
    May 8, 2017 at 11:49
  • Thanks! That helped a lot. At first I didn't really get your point but eventually i understood. The error with my code was strange. Essentially I had made an array with every index in that array with the entire linked lst of the dict. so, the array (at the time) was size 50, so it had 50 duplicates of the entire dict! The issue with the smaller dicts was due to a poorly implemented counting function.
    – Matt
    May 11, 2017 at 5:32
  • That's not exactly it. You have a single linked list, and for each word, update a single entry of your array to the currently first node of the linked list. So eventually you'll have references to the linked list in all of the array elements, but they point to different nodes, so you would get the first words on all entries, but the last only on one. But let's agree on that it's not what a hash map is supposed to do.
    – Blauelf
    May 11, 2017 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .