Skip to content

Instantly share code, notes, and snippets.

/dictionary.c

Created Mar 13, 2016
Embed
What would you like to do?
Issue
/**
* dictionary.c
*
* Computer Science 50
* Problem Set 5
*
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
unsigned int hash(const char* word);
#define TABLESIZE 100
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}
node;
node* hashtable[TABLESIZE];
unsigned int wordcount = 0;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// create a cursor to iterate through the linked list
node* cursor = hashtable[hash(word)];
// transverse through the list
while (cursor)// -> next != NULL)
{
// if equal non case sensive
if(strcmp(cursor -> word, word) == 0 ||
strcmp(cursor -> word, word) == 32 ||
strcmp(cursor -> word, word) == -32)
{
// word found
return true;
}
// if they don't match, try the next one in the list
else
{
// try next word
cursor = cursor -> next;
}
}
// if it ever equals NULL, the list has ended and the word couldn't be found
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// open the dictionary file
FILE* file = fopen(dictionary, "r");
// check for errors opening the dictionary
if (file == NULL)
{
printf("Couldn't open the file\n");
return false;
}
// create a buffer for the word
char buffer[LENGTH + 1];
// Prepare the array
for(int i = 0; i < TABLESIZE; i++)
{
hashtable[i] = NULL;
}
// do the thing
for(int i = fscanf(file, "%s\n", buffer); i != EOF; i = fscanf(file, "%s\n", buffer))
{
// the hash of the buffer is the index in the hashtable
int index = hash(buffer);
// if the index is NULL, make it point to a new node
if (hashtable[index] == NULL)
{
// create the new node
node* newnode = malloc(sizeof(node));
// check for error
if (newnode == NULL)
{
printf("Couldn't add the word %s\n", buffer);
return false;
}
// append the first node in this index
newnode -> next = NULL;
strcpy(newnode -> word, buffer);
hashtable[index] = newnode;
wordcount++;
}
// else it's already pointing, let's do something different which is
else
{
// create a new node
node* new_node = malloc(sizeof(node));
// check for errors
if (new_node == NULL)
{
printf("Couldn't add the word %s\n", buffer);
return false;
}
// new node has the current word
strcpy(new_node -> word, buffer);
// points to the old head
new_node = hashtable[index];
// this new node will be the head
hashtable[index] = new_node;
wordcount++;
}
}
fclose(file);
return true;
}
unsigned int hash(const char* word)
{
unsigned int hash = 0;
for(int i = 0, b = strlen(word); i < b; i++)
{
hash += word[i];
}
return hash % TABLESIZE;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
return wordcount;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
for(int i = 0; i < TABLESIZE; i++)
{
node* cursor = hashtable[i];
while (cursor != NULL)
{
node* temp = cursor;
cursor = cursor -> next;
free(temp);
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment