0

enter image description here // Implements a dictionary's functionality

#include <stdbool.h>

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

// 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 = 17576;
int count_word = 0;

// Hash table
node *table[N];

//node *cursor = NULL;
//node *temp = NULL;

FILE *file = NULL;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *cursor = table[hash(word)];

    while (cursor->next != NULL)
    {
        if(strcasecmp(cursor->word, word) == 0)
        {
            return true;
        }
        else
        {
            cursor = cursor->next;
        }
    }
    
        if (cursor->next == NULL)
        {
            if(strcasecmp(cursor->word, word) == 0)
            {
                return true;
            }     
        }

    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int x = 0, y = 0, z = 0;

    //converting the first 3 letters of any word to an int representing number of backet
    if (strlen(word) >= 1)
    {
        if (isalpha(word[0]) != 0)
        {
           x = tolower(word[0]) - 'a';
        }
        else
        {
            x = 0;
        }  
    }

    if (strlen(word) >= 2)
    {
        if (isalpha(word[1]) != 0)
        {    
           y = tolower(word[1]) - 'a';
        }
        else
        {
        y = 0;
        }
    }
    if (strlen(word) >= 3)
    {
        if(isalpha(word[2]) != 0)
        {    
            z = tolower(word[2]) - 'a';
        }
        else
        {
            z = 0;
        }
    }


    //returning a valid index
    if((x * 26 + y * 26 + z) > N)
    {
        return ((x * 26 + y * 26 + z) % N);
    }
    else
    {
        return (x * 26 + y * 26 + z);
    }

}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word[LENGTH + 1];
    file = fopen(dictionary,"r");
    unsigned int i;

    if (file == NULL)
    {
        unload();
        return false;
    
    }
    //node *n = NULL;

    for (int j = 0 ; j < N ; j++)
    {
        table[j] = NULL;
    }

    while (fscanf(file, "%s", word) != EOF)
    {
       node *n = malloc(sizeof(node)); 
       if (n == NULL)
       {
           unload();
           fclose(file);
           return 1;
       }
       strcpy(n->word , word);
       n->next = NULL;
       i = hash(word);
       count_word++;
       node* head = table[i];
       if (head == NULL)
       {
          table[i] = n;
       }
      else
      {
      n->next = table[i];
      table [i] = n;
  }
}
fclose(file);
return true;
}

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

    return count_word;
}

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

You must log in to answer this question.

Browse other questions tagged .