0
// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include "dictionary.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 = 52;

// Hash table
node *table[N];
// global counter
unsigned int counter = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *checker;
    int bucket = hash(word);

    checker = table[bucket];
    while (checker != NULL)
    {
        if (strcasecmp(checker->word, word) == 0)
        {
            return true;
        }
        else
        {
            checker = checker->next;
        }
    }

    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int index = 0;
    int value = 0;
    // sum ascii values
    for (int i = 0; word[i] != '\0'; i++)
    {
        index += tolower (word[i]);
    }
    // to stay within the hash table
        value = index % N;

     return value;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word[LENGTH + 1];

  FILE *dico = fopen(dictionary, "r");
  if (dico == NULL)
  {
      return false;

  }
    while (fscanf(dico, "%s", word) != EOF)
    {
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            return false;
        }
        else
        {
            strcpy (new_node->word, word);

            unsigned int place = hash(new_node->word);

            if (table[place] == NULL)
            {
                table[place] = new_node;
                new_node->next = NULL;
            }
            else
            {
                new_node->next = table[place];
                table[place] = new_node;
            }
        }
        counter++;
    }
    printf("%i", counter);
     fclose(dico);
    return true;
}

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

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

    }
    return true;
}
0

so at the unload function:

  1. inside the while loop you initialized (cursor = table[i]), the cursor will point at what table[I] is pointing at in the list.
  2. then you meant to re-initialized -but you used the wrong syntax it should be like this- cursor = curosr->next
  3. 3- then temp is freed

    until now other than the syntax everything is good.

But, after the first iteration things start to go to the wrong way, it will re-initialized (cursor = table[I]) which is wrong because what ever table[I] was pointing at is already have been freed, so the rest of the elements in the list are orphaned.

so focus on the while loop and where to initialize the cursor and what condition to put to make sure that every element in the list is freed.

++ there are many good hash functions you can find, make sure to find a good one.

GoodLuck Yasir!

You must log in to answer this question.

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