0
#include <stdbool.h>
#include <stdlib.h>
#include "dictionary.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>

int wordCount = 0;

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];



// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int index = hash(word);
    node *temp = table[index];
    
    
    while (temp != NULL)
    {
        if (strcasecmp (word, temp->word) == 0)
        {
            return true;
        }
        else
        {
            temp = temp->next;
        }
    }

    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int result = 0;
    
    for (int i = 0; i < word[i]!='\0'; i++)
    {
        result += tolower(word[i]);
    }
    
    return (result % N);
    
    
}


// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *d = fopen (dictionary, "r");
    if (d == NULL)
    {
        printf("file not found");
        return false;
    }
    for (int i = 0; i < 26; i++)
    {
        table[i] = NULL;
    }
    
    char temp[LENGTH+1];
    node *n = malloc(sizeof(node));
      if (n == NULL)
        {
            return false;
        }
    
    while (fscanf (d, "%s", temp) != EOF)
    {
        strcpy(n->word, temp);
        
        int index = hash(temp);
        
        if (table[index] == NULL)
        {
            n->next = NULL;
            table[index] = n;
            wordCount++;
        }
        else
        {
            n->next = table[index];
            table[index] = n;
            wordCount++;
        }
    }
    fclose(d);
    free (n);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
   return wordCount;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < 26; i++)
    {
        node *cursor = table[i];
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free (temp);
        }
        table[i] = NULL;
    }
    return 0;
}

I have been looking at this problem all week changing bits and pieces but I can't get it to work at all! I've tried debugging in each of the different functions but still can't seem to source what is actually wrong. Any help would be much much appreciated.

1

My first advice: insert return true; into the beginning of unload() so that it does nothing else. This will let you work on everything else.

Next, insert #include <strings.h> to correct the compile error. Note the plural form of string. ;-)

In load() the code creates a single node n. The problem is that it's reusing the node over and over and over. That means you get one word in your dictionary at best. Even worse, it is freed at the end of load, so there's nothing in the dictionary.

Two fixes in load(). First, declare n before the start of the loop, but do the malloc inside the loop. (The code is allocating the memory for the node, loading the node and then moving the address of the node into the tree. Once the address of the node has been moved into the tree, the n pointer can be used to process the next node/word. )

Second, nothing should be freed in load. All the nodes need to persist throughout the program until deleted/freed by unload.

Back to unload(). This one's easy. The double free was being caused by something fixed above. I'll let you figure out the details on your own. It'll be a good exercise for you. ;-)

However, there's no need to set vaules in table[] to null. The memory has been freed and table will be released by the internal cleanup housekeeping done by C when the program shuts down. Having said that, it doesn't hurt to reset a now-invalid address, especially if it has the potential to be reused in a program. (That possibility doesn't exist here.)

Problems solved. ;-)

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

| improve this answer | |

You must log in to answer this question.

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