0
// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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 = 676;

// Hash table
node *table[N];

// size of dictionary
int lines = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    return false;
}

// Hashes word to a number
// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    int a,b,c;
    if ('A' <= word[0] && word[0] <= 'Z')
    {
        a = (word[0] % 65);
    }
    else
    {
        a = (word[0] % 97);
    }
    if ('A' <= word[1] && word[1] <= 'Z')
    {
        b = (word[1] % 65);
    }
    else
    {
        b = (word[1] % 97);
    }
    c = (a * 26) + b;
    return c;
}
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    FILE *dictionary1;
    dictionary1 = fopen (dictionary, "r");
    char a[46];
    char b;
    int n = 0;
    int j;
    node *f = NULL;
    if (dictionary1 == NULL)
    {
        return false;
    }
    else
    {
        while((b = fgetc(dictionary1)) != EOF)
        {
            if(b == '\n')
            {
                lines++;
            }
        }
        fseek(dictionary1, 0, SEEK_SET);
        for(int i = 0; i < lines; i++)
        {
            a[0] = fgetc(dictionary1);
            while(a[n] != '\n')
            {
                a[n + 1] = fgetc(dictionary1);
                n++;
            }
            n = 0;
            j = hash(a);
            node* tmp = table[j];
            while(true)
            {
                if(tmp == NULL)
                {
                    break;
                }
                else
                {
                    tmp = tmp -> next;
                }
            }
            f = malloc(sizeof(node));
            if (dictionary1 == NULL)
            {
                fclose (dictionary1);
                return false;
            }
            f -> next = NULL;
            int y =strlen(a);
            for (int k = 0; k < y; k++)
            {
                f -> word[k] = a[k];
            }
            if (i == 0)
            {
                table[j] = f;
            }
            else
            {
                f -> next = table[j];
                table[j] = f;
            }
            f = NULL;
        }
        fclose (dictionary1);
        return true;
    }
}

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

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

double free or corruption (fasttop)
Aborted
5
  • Did you try to remove the last 'if - else' sentences? The problem coul'd be that you're trying to acces to a part of the memory that is already freed. – Tritum May 30 '20 at 11:15
  • Removing if-else works for dictionaries/small but not for dictionaries/large – Vsjain May 30 '20 at 13:00
  • I think you should look in other places (load function probably). I can't see anything wrong here. – Tritum May 30 '20 at 13:05
  • can you post your dictionary.c file? you can also debug it by following this instruction – stensal May 30 '20 at 18:06
  • I changed the hash function and now i am having a problem with both small and large. It works with small if I use ``` c = (a * b) - 1; ``` which is wrong. – Vsjain Jun 1 '20 at 8:08

You must log in to answer this question.

Browse other questions tagged .