0

Not sure if rest of code works correctly but I cannot get the load function loading more than 1 word. It only loads the first word from the dictionary and no more. Can somebody please give some hint where my code is wrong?

bool load(const char* dictionary)

{
// open dictionary
FILE* filedict = fopen(dictionary, "r");

// check if the file is empty
if (filedict == NULL)
{
    printf("Could not open file.");
    unload();
    return false;
}
// initialize each hashtable basket to null
for (int i = 0; i < TABLESIZE; i++)
{
    hashtable[i] = NULL;
}

// load words until eof, hash each and place in linked list
while(!feof(filedict))
{
    node* new_node = calloc(1, sizeof(node));
    fscanf(filedict, "%s", new_node->word);
    {
        printf("start load function\n");
        printf("the word in dictionary is %s", new_node->word);
        int basket = hash_function(new_node->word);

        if (hashtable[basket] == NULL)
        {
            // it is the first word for this basket
            hashtable[basket] = new_node;
            wordcount++;
        }
        else
        {
            // linked list was already started, put word at end of list

            new_node -> next = hashtable[basket] -> next;
            hashtable[basket] -> next = new_node;
            wordcount++;

        }
    }
    fclose(filedict);
    return true;
}
return false;
}
0
  • if you couldn't open the dictionary file at the first place, why are you trying to unload it?

  • using feof this way could lead to problems. see What is the difference in behavior between fread and feof?

  • using fscanf is not recommended (remember why?)

  • I doubt that your load function only loads a single word.

if hashtable[basket] is an empty pointer, you're correctly setting it to new_node. if hashtable[basket] != NULL, you're setting new_node->next to hashtable[basket]->next then setting hashtable[basket]->next to new_node. so for every node to be inserted in the list whose root node is hashtable[basket], except for the first node, you're inserting this node as the second node in the list. see the following:

// calloc memory for new_node and fill the string member with data
   +-------+
   + data2 +
   +-------+
   + next  ===>NULL
   +-------+
   new_node

// a case where hashtable[basket] is not NULL
   +-------+         +-------+     
   + data0 +         + data1 +
   +-------+         +-------+
   + next  ========> + next  ===> NULL
   +-------+         +-------+
hashtable[basket]

// new_node->next = hashtable[basket]->next
   +-------+    +-------+     
   + data2 +    + data1 +
   +-------+    +-------+
   + next  ===> + next  ===> NULL
   +-------+    +-------+
   new_node

// hashtable[basket]->next = new_node
   +-------+         +-------+    +-------+     
   + data0 +         + data2 +    + data1 +
   +-------+         +-------+    +-------+
   + next  ========> + next  ===> + next  ===> NULL 
   +-------+         +-------+    +-------+
hashtable[basket]    new_node

of course there's no point of inserting each node as the second node and I suppose you meant to insert each node at the beginning of the list. so I'll leave fixing this to you. a pencil and a paper may help.

| improve this answer | |
  • Thanks Kareem! I meant adding the word at the end of the list, but then still, your explanation is really helpful! fscanf was given so I figured in this case it can work out. But i'll look into other options. Thanks!!!! – Inge May 5 '16 at 20:25

You must log in to answer this question.

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