I'm looking to make my code faster for loading a hash table. Any pointers would be appreciated. Below are my includes, the node def and the loading code.
The dictionary loaded could be any *.txt file. Its pre processed to contains just lower case words sans punctuation.
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
// define hash table structure
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
bool load(const char* dictionary)
{
// open dictionary and check for NULL
FILE* fpdict = fopen(dictionary, "r");
if (fpdict == NULL)
{
printf("Could not open %s.\n", dictionary);
unload();
return false;
}
// read in txt file
else
{
node* new_node = malloc(sizeof(node));
for ( ; fscanf(fpdict, "%s", new_node->word) != EOF;
new_node = malloc(sizeof(node)) )
{
// call hash function to get key
key = hash(new_node->word, strlen(new_node->word), TABLESIZE);
// check if hashtable key is NULL
if (hashtable[key] == NULL)
{
hashtable[key] = new_node;
new_node->next = NULL;
}
// if linked list exists, insert it in order
else if (hashtable[key] != NULL)
{
// point to first hashtable entry
node* head = hashtable[key];
// set crawler to head
node* crawler = head;
// check where to insert
// insert word at end and set pointer to NULL
while (crawler->next != NULL)
{
crawler = crawler->next;
}
new_node->next = NULL;
crawler->next = new_node;
}
wordCount += 1;
}
fclose(fpdict);
free(new_node);
return true;
}
return false;
}
hash()
it's not possible to give a useful answer. \$\endgroup\$