0

I think my load function is pretty close, but I cannot seem to get it to work properly, I built a function to read the data and I saw it's only storing one word in each bucket. Any ideas on what's going on? Thanks!

enter code here #include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
void printHash();
 // Returns true if word is in dictionary else false
int main(void)
{
load("dictionaries/small");
}

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
int count = 0;

bool load(const char *dictionary)
{
// TODO
// For each word in the dictionary, store it in a data structure
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
    printf("Could not load file.");
    return false;
}
printf("%s\n", dictionary);
char *word[40];
node *hashtable[26];
// node *head[4];

//init hashtable to null
for (int i = 0; i < 26; i++)
{
   hashtable[i] = NULL;
}

while(fscanf(file, "%s", *word) != EOF)
{
    node *temp = malloc(sizeof(node));
    node *new_node = malloc(sizeof(node));
    if (new_node == NULL)
    {
        //unload();
        free(hashtable);
        free(new_node);
        return false;
    }

    else

    {

        strcpy(new_node->word, *word);

        int value = hash(new_node->word);


        if(hashtable[value] == NULL)
        {
            hashtable[value] = new_node;
            new_node->next =NULL;
            count++;

        }
        else
        {
            temp = temp->next;
            hashtable[value] = new_node;
            new_node->next = temp;

            count++;

        }
    }

}
printf("%i\n",count);
printHash(hashtable);
// Close file
fclose(file);

// Return bool
return true;
}


void printHash(node *hashtable[26])
{
node *ptr = malloc(sizeof(node));
for(int i=0; i < 26; i++)
{
    if(hashtable[i] != NULL)
    {
        ptr->next = hashtable[i];
         while(ptr->next != NULL)
        {
                printf("%s", hashtable[i]->word);
                printf("\n");
                ptr = ptr->next;

        }
    }
}
}
1

Let's look at this code:

node *temp = malloc(sizeof(node));

...

    else
    {
        temp = temp->next;
        hashtable[value] = new_node;
        new_node->next = temp;
        count++;
    }

temp is created and initialized with the address of a node with a malloc command. That means that the new node is uninitialized, so temp->next contains garbage data, not a valid address and not NULL. (Hint: check out calloc ).

Next, we drop through to the else clause for the second and later words for a given hash value. The following events happen:

  1. Temp is set to temp->next, a garbage address.
  2. the new node is assigned to the initial bucket in the linked list, i.e., hashtable[value].
  3. new_node->next is set to the garbage value in temp.

What doesn't happen is just as important. At no point is the current value in hashtable[value] saved. It is just overwritten. The node still exists, but the address is lost. It makes for a nasty memory leak too.

The code needs to be restructured. A new node can easily be inserted at the beginning without the use of a temp node var. Just set new_node->next to the head of the list and insert new node into the array.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • So this does help a lot, I do have one question though. When you say "Just set new_node->next to the head of the list and insert new node into the array." I'm not sure I understand how to set the new_node->next to the head. Any help there would be very appreciated. Thanks! Feb 28 '19 at 23:43
  • Once a node is created and populated, it is added to the appropriate linked list. The "head" of each linked list is stored in your hashtable[ ] array. You should be able to figure out the rest.Isn't there a video short about this?
    – Cliff B
    Feb 28 '19 at 23:51

You must log in to answer this question.

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