Just trying to get the simple version of the hash function to work for now (haven't implemented the hash function logic yet, just wanna fix the memory errors before getting to the actual logic) but can't figure where the memory error is arising.
In my load function, I open up the dictionary file and initialize my hash table to set all pointers to NULL. Then, I use fscanf to scan the dictionary file, create a node *n for each word in the dictionary, and copy the word into this node.
If table[index] == NULL, then I set both table[index] and a node called head equal to node n, and set the next address equal to NULL. Otherwise, I set the next node as table[index] and table[index] as the current node, n.
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
//represents the number of words in the dictionary
int word_count = 0;
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// TODO: Choose number of buckets in hash table
const unsigned int N = 26;
// Hash table
node *table[N];
// initialize hash table (set all values to NULL)
// reference video: https://youtu.be/2Ti5yvumFTU
void init_table()
{
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
}
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
//obtain the index of the word in the hash
int node_index = hash(word);
//initiate cursor to point to the first node of the LL
node *cursor = table[node_index];
//traverse through the LL searching for the word
while (cursor != NULL)
{
if (strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO: Improve this hash function
return ((toupper(word[0]) - 'A') % N);
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
char *word = NULL;
int node_index = 0;
// Open input file
FILE *file = fopen(dictionary, "r");
//check if file exists
if (file == NULL)
{
return false;
}
init_table();
//count the number of words in the dictionary
word_count = fscanf(file, "%s", word);
node *head;
//loop through file for each word
while (word_count != EOF)
{
//assign memory for a new node
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
//copy the scanned word into the created node
strcpy(n->word, word);
//get the hash index of the node
node_index = hash(n->word);
if (table[node_index] == NULL)
{
head = table[node_index] = n;
n->next = NULL;
}
// otherwise set next node as table[index], table[index] as current node n
else
{
n->next = head;
table[node_index] = n;
}
}
return true;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
node *temp = table[i];
while (cursor != NULL)
{
cursor = cursor->next;
free(temp);
temp = cursor->next;
}
}
return true;
}
A different file has the main function that links to this file.
Any help would be appreciated, thanks!