I do really need help on LOAD. I can not figure out what exactly is going wrong with my code and if I am on the right track or not. I would appreciate any help. Besides, I'm getting an error: "multiple definition of `hash_function'" when I try to make a shorter variable "i" which is supposed to numerate the linked lists.
Here's the code in dictionary.c:
bool load(const char* dictionary)
{
// open dictionary file
FILE *dfp = fopen(dictionary, "r");
if (dfp == NULL)
{
printf("Could not open %s.\n", dictionary);
unload();
return 1;
}
while (!feof(dfp))
{
// malloc for a new node
node* new_node = malloc(sizeof(node));
// read in a word to the node
fscanf(dfp, "%s", new_node -> word);
// hush function variables
int i = hash_function(new_node -> word);
// put the new node to the specified by hash-function bucket
if (hashtable[i] == NULL)
{
hashtable[i] -> next = new_node;
}
else
{
node *tmp_node = malloc(sizeof(node));
tmp_node -> next = hashtable[i] -> next;
hashtable[i] -> next = new_node;
}
}
return true;
}
and here is the dictionary.h:
bool load(const char* dictionary);
// declaration of hash function
int hash_function(char *key)
{
// hash on first letter of string
int hash = toupper(key[0]) - 'A';
return hash % SIZE;
}
// structure for linked lists
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
// the hash table
node* hashtable[SIZE];