Skip to content

Instantly share code, notes, and snippets.

@chrisdel101
Last active May 15, 2018 01:32
Show Gist options
  • Save chrisdel101/50301fd66bfff67f6c45750f41f28cba to your computer and use it in GitHub Desktop.
Save chrisdel101/50301fd66bfff67f6c45750f41f28cba to your computer and use it in GitHub Desktop.
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <xlocale.h>
#include "dictionary.h"
int hashValue(const char *word);
//get original word and case
//hash that for index
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
node *hash_table[HASHTABLE_SIZE] = {NULL};
int count = 0;
//Used this post as reference - https://cs50.stackexchange.com/questions/27133/pset5-speller-load-function-not-working-properly
//Used this post for my hash function - https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return count;
}
bool check(const char *word)
{
int len = strlen(word);
char arr[len + 1];
// SET WORD TO LOWER
for (int i = 0; i < strlen(word); i++)
{
arr[i] = tolower(word[i]);
}
arr[len] = '\0';
// HASH VALUE
// hash to get bucket/array index of original with case
int hash_index = hashValue(arr);
// set head to first node in bucket/array - node
node *head = hash_table[hash_index];
node *cursor = head;
while (cursor != NULL)
{
if (strcmp(arr, cursor->word) == 0)
{
return true;
}
cursor = head->next;
head = cursor;
}
return false;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *inptr = fopen(dictionary, "r");
node *mem;
char temp[LENGTH + 1];
// load dictionary to test
while (fscanf(inptr, "%s", temp) != EOF)
{
// malloc mem
mem = malloc(sizeof(node));
if(mem == NULL)
{
return 1;
}
// copy line into node's word
strcpy(mem->word, temp);
// hash it
int hash_index = hashValue(temp);
// printf("load word: %s ", temp);
// printf("load index %i\n", hash_index);
// if null, head is empty
if (hash_table[hash_index] == NULL)
{
// insert at head of linked list
// same as head = mem
hash_table[hash_index] = mem;
// make next null
mem->next = NULL;
count++;
}
else
{
// set next to current head
mem->next = hash_table[hash_index];
// reassign head to new node
hash_table[hash_index] = mem;
count++;
}
}
fclose(inptr);
// printf("count %i", count);
size();
// puts("made it to end");
return true;
}
// unloads a dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < HASHTABLE_SIZE; i++)
{
node *cursor = hash_table[i];
// node *cursor = head;
if (cursor != NULL)
{
// set temp to cursor
node *temp = cursor;
// point cursor at next node
cursor = cursor-> next;
free(temp);
}
}
return true;
}
int hashValue(const char *word)
{
unsigned int hash = 0;
for (int i = 0, n = strlen(word); i < n; i++)
{
hash = (hash << 2) ^ word[i];
}
// returns a number
return hash % HASHTABLE_SIZE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment