I've been working on speller (implementing a hashtable) for quite a while now and I'm stuck...my code compiles but I'm assuming there's a run-time error. I'm not sure what to make of my error message, and I'm not sure my logic is even correct; could someone take a look at my code and explain what I need to improve upon, as well as decipher the error? Help is very much appreciated.
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cs50.h>
#include "dictionary.h"
#define N 26
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Represents a hash table
node *hashtable[N];
// Hashes word to a number between 0 and 25, inclusive, based on its first
letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}
// Keeps track of number of words
int word_count = 0;
int loaded = 1;
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Initialize hash table
for (int i = 0; i < N; i++)
{
hashtable[i] = NULL;
}
// Open dictionary
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Buffer for a word
char word[LENGTH + 1];
for (int i = 0; i < N; i++)
{
while ((fscanf(file, "%s", word) != EOF) &&
(hash(word) == i))
{
// Creates a new node
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
unload();
return false;
}
// Copies the word into the word portion of the node, increments
number of words
strcpy(new_node->word, word);
word_count++;
}
}
fclose(file);
loaded = 0;
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
unsigned int i = hash(word);
node *cursor = hashtable[i];
while (cursor != NULL)
{
if (strcasecmp(word, cursor->word) == 0)
return true;
else
cursor = cursor->next;
}
// If the word doesn't show up, then it is incorrect.
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *cursor = hashtable[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor->next;
free(temp);
}
}
return true;
}
There are no syntactical errors, I don't think, but when I execute "make dictionary" this happens, after a bunch of repetitive lines are printed:
(.text+0x20): undefined reference to `main' clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [dictionary] Error 1
Any help is very much appreciated. Thank you!