When I assign table[index3] = n; what happens is that the value of the word receives a memory address and I can't follow table[index]->next to achieve the old n->word; This shouldn't happen.
// Implements a dictionary's functionality
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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 = 6359;
// Hash table
node *table[N];
// Declaration of variable that counts words loaded from dictionary
int howmany = 0;
// Declararation of tmp pointer
node *tmp = NULL;
// Declaration of index used to address a bucket from a hash
unsigned int index2 = 0;
unsigned int index3 = 0;
// Declare variable key
unsigned int key = 0;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
bool checked = false;
index2 = hash(word);
tmp = table[index2]->next;
while (tmp->next != NULL)
{
if (strcasecmp(tmp->word, word) == 0)
{
checked = true;
}
// Updates tmp to next word in list
tmp = tmp->next;
}
return checked;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// Assigns variables
char *position = "00zyxwvutsrqponmlkjihgfedcba'";
// Calculate a key for the word
for (int i = 0; word[i] !='\0'; i++)
{
for (int j = 0; j < 29; j++)
{
if((int)word[i] == (int)position[j])
{
key = key + (int)word[i] * i * j * 31 * 7;
}
}
}
printf("key is : %i\n", key); /////test
// Recalculates values bigger than N
if (key > N)
{
key = key % N;
}
/// Test-----------------------------------------
printf("Key is: %i\n", key);
return key;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
char *word[46];
// Load words in hash table
while (fgets(*word, 46, file) != NULL)
{
printf("word is: %s", *word); // test------------
// Hash the key of a certain word and translates to an array's index
index3 = hash(*word);
printf("index3 é : %u\n", index3); // test----------------
// Allocate memory for a node
node *n = malloc(sizeof(node));
if (n == NULL)
{
return 1;
}
strcpy(n->word, *word);
n->next = NULL;
if (table[index3] == NULL)
{
table[index3] = n;
printf("table[index3]after: %p\n", table[index3]->next); // test
printf("table[index3]word: %p\n", table[index3]->word); //test
}
else
{
// Scan end of list and update with 'n'
tmp = malloc(sizeof(node));
tmp = table[index3]->next;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = n->next;
strcpy(tmp->word, n->word);
}
// Updates number of words as loads each one
howmany++;
}
if (howmany > 0)
{
return true;
}
else
{
return false;
}
fclose(file);
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return howmany;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// Declaration of temporary pointer and variable
node *cursor = NULL;
int is_not_null = 0;
// Free memory for list in each bucket
for (int i = 0; i < N; i++)
{
if(table[i] != NULL)
{
*tmp = *table[i];
*cursor = *table[i];
while(cursor->next != NULL)
{
cursor = cursor->next;
free(tmp);
tmp = cursor;
}
free(tmp);
free(cursor);
}
if (table[i] != NULL)
{
is_not_null++;
}
}
printf("is_not_null: %i\n",is_not_null); //-----------teste--------------------
// Verify if memory was freed and return a boolean value
if(is_not_null == 0)
{
return true;
}
else
{
return false;
}
}