I am building my load() function and I'm wondering if I'm building it so that it is indeed storing the dictionary words correctly.

I tried printing both:

printf("%s\n", new_node->word); //to see if I'm reading all words

printf("%s\n", hashtable[index]->word); //to see if the head of each linked list is correct.

For printf("%s\n", hashtable[index]->word); I was assuming that it might print out just 26 words [the first letter of each list a...z] but rather it printed out many words.

I know I'm still struggling to fully grasp how to populate the dictionary.

Any feedback on the below code would be helpful and any insights into how to check if a hashtable is correct [ie. how can I print out or test the structure to know it's correct]?

// Loads dictionary into memory, returning true if successful, false if not
bool load(const char *dictionary)
    //open dictionary file
    FILE *inptr = fopen(dictionary, "r");

    //declare temp array for word
    char word[LENGTH + 1];

    //read each word in dictionary til end of file
    //for every line we read in dictionary
    //we call it "word" and read it until as line
    while (fscanf(inptr, "%s ", word) != EOF)
        //create a node and allocate memory for each word
        node *new_node = malloc(sizeof(node));

        // memset(new_node, 0, sizeof(node));

        //if empty...
        if (new_node == NULL)
            //speller quits
            return false;

        //hash function provides location of word
        int index = hash(word);

        //if this is the start of table
        if(hashtable[index] == NULL)
          head = malloc(sizeof(node));
          if(head == NULL)
             printf("null head!");
             exit (1);

        //add word into node...
        strcpy(head->word ,word);

        head->next = NULL;

        //tell head what word it represents
        hashtable[index] = head;


            //copy each word from dictionary into node
            strcpy(new_node->word, word);

            //test if built correctly
            //printf("%s\n", new_node->word);

            //declare hashed index of word
            index = hash(word);

            //thinking of hashtable[index] as head
            //switch it to new node
            hashtable[index] = new_node;

            //is hashtable the first of the letter?
            //printf("%s\n", hashtable[index]->word);

            //make the next point to null
            new_node->next = NULL;

            //increment counter for each word in dictionary to return in SIZE
    //close dictionary file
    return true;

NOTE: so far my check() function, which I'm still working on, prints out every word as mispelled, so I believe there an error in load(). And I was aiming to follow advice from this excellent response from another user.


1 Answer 1


Start with a smaller dataset (dictionary). Keeping track of 143K+ nodes is a job for a computer, not a human. And remember, the same file can be used as both dictionary and text, so dictionaries/small might be a good place to start. That should be manageable in debug50 (or even with printf debugging).

When program reaches the else block (in load) it has populated hashtable[index] as the first node. When this line hashtable[index] = new_node; executes, that node is lost..... forever. hashtble[index] will end up pointing to the last node in the index, which points to NULL.

If the new_node points to the head node (instead of NULL), no nodes will get lost. (Of course that has to be executed before hashtable[index] changes). And since the (original) head node points to NULL, the hashtable will be properly "terminated".

You must log in to answer this question.

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