0

// Implements a dictionary's functionality

include

include

include

include "dictionary.h"

include

include

include

// Number of buckets in hash table

define N 120000

//maximum length of word

define LENGTH 45

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

// Hash table node *table[N];

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

// Hashes word to a number //https://gist.github.com/OxiBo/91abfc3d9ff7c8fcfa281895111314db unsigned int hash(const char* word) { unsigned int hash = 0; for (int i=0; word[i]!= '\0'; i++) { hash = 31*hash + word[i]; } return hash % N; }

// Loads dictionary into memory, returning true if successful else false bool load(const char *dictionary) {
FILE *dict=fopen(dictionary, "r"); char words[LENGTH + 1];

// check if the opened file is empty
if (dict==NULL)
{
    return false;
}
while(!feof(dict))
{
    //read every word in the dictionary file on to a charater array
    fscanf(dict,"%s", words);        

    //create new node
    node *n=malloc(sizeof(node));
    strcpy(n->word, words);
    n->next=NULL;
    if (n==NULL)
    {
        return false;
        break;
    }

    //return index value from hash function
    unsigned int  k=hash(n->word);

    //link list of words on to the hashtable  
    //if the hash table is empty point towards the node     
    if (table[k]==NULL)
    {
        table[k]=n;
        word_count++;
    }
    //if the hash table is already filled up create a temporary node to link the next node
    else if(table[k]!=NULL)
    {
        n = table[k];
        n->next = n;
        table[k] = n;
        word_count++;
    }
}   
fclose(dict);
return false;

}

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

// Unloads dictionary from memory, returning true if successful else false bool unload(void) { for (int i = 0; i

    while (cursor!=NULL)
    {
        node *tmp = cursor;
        cursor = cursor->next;
        free(tmp);
    }
}
return false;

}

  • Have a look in speller.c. What does it do with the return value of the call to load()? – DinoCoderSaurus Jun 14 at 20:24
  • Dino, make that an answer! – Cliff B Jun 14 at 22:01
  • the return was false that is why...thank you. @DinoCoderSaurus – Exodus Joel Jun 15 at 15:49
  • the word counter is not working properly. size function retruns additional number of words. – Exodus Joel Jun 15 at 17:29

You must log in to answer this question.

Browse other questions tagged .