I´ve been stuck in pset5 for a little while. I was wondering if someone would be able to take a loot at it? Fow now all my hash function does is look at the first letter of a word. I will of course create a better way of hashing later, but I first wanted to have a simple solution that works--and this current code does not work, not even for the small dictionary and a small text. I am very lost as to what is wrong. Many thanks in advance for the help!
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include "dictionary.h"
#include <string.h>
#include <stdlib.h>
#include <strings.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 = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
//hashing word to find where in the hash table to check for the word
int check_location = hash(word);
for (node *cursor = table[check_location]; cursor != NULL; cursor = cursor->next)
{
if (strcasecmp(word, cursor->word) == 0)
{
return true;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
char first = word[0];
if (65 <= first && first <= 90)
{
return (first - 65);
}
else if (97 <= first && first <= 122)
{
return (first - 97);
}
//I don't think there will be another else... But if there is, let's assign it to 0
return 0;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
return false;
}
char current_word[LENGTH + 1];
//or the below?
//char *current_word = malloc(LENGTH + 1);
*size_counter = 0;
for (int c = fscanf(dict, "%s", current_word); c != EOF; c = fscanf(dict, "%s", current_word))
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
//not enough memory
return false;
}
//Assign the word we are reading to the 'word' component of n
strcpy(n -> word, current_word);
//'next' component of n points to NULL
n -> next = NULL;
//gets hash value for current_word and stores it as h_index
int h_index = hash(current_word);
//'next' component of n now points to the contents of the hash table at our desired location (h_index)
n-> next = table[h_index];
//insert n to table
table[h_index] = n;
//increment size counter (one more word was read from dictionary)
++*size_counter;
//adding this does not seem to solve the problem:
//free(current_word);
}
//NOT SURE IF THIS NEEDED
//free(current_word);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size()
{
return *size_counter;
//free(size_counter);
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
int table_size = sizeof(table);
for (int i = 0; i < table_size; i++)
{
for (node *cursor = table[i]; cursor != NULL; cursor = cursor->next)
{
node* tmp = cursor;
free(tmp);
}
}
return true;
}