Last active
April 4, 2018 16:31
-
-
Save prateek-parashar/c585555e4f99c1b3ef025932cfd80180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implements a dictionary's functionality | |
#include <stdbool.h> | |
#include <strings.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include "dictionary.h" | |
unsigned long hash_function(const char *word); | |
typedef struct node | |
{ | |
char word[LENGTH + 1]; | |
struct node *next; | |
} | |
node; | |
node *hashtable[15000]; | |
int j = 0; | |
// Returns true if word is in dictionary else false | |
bool check(const char *word) | |
{ | |
int search_index = 0; | |
search_index = hash_function(word); | |
node *cursor = hashtable[search_index]; | |
if (hashtable[search_index] == NULL) | |
{ | |
return false; | |
} | |
else | |
{ | |
while (cursor != NULL) | |
{ | |
if (strcasecmp(cursor -> word, word) == 0) | |
{ | |
return true; | |
} | |
cursor = cursor -> next; | |
} | |
} | |
return false; | |
} | |
// Loads dictionary into memory, returning true if successful else false | |
bool load(const char *dictionary) | |
{ | |
// TODO | |
char *dictword = malloc(46 * sizeof(char)); | |
FILE *dict = fopen(dictionary, "r"); | |
if (dict == NULL) | |
{ | |
return false; | |
} | |
while (fscanf(dict, "%s", dictword) != EOF) | |
{ | |
//creating space for new node | |
node *new_node = malloc(sizeof(node)); | |
//checking for null pointer | |
if (new_node == NULL) | |
{ | |
unload(); | |
fclose(dict); | |
return false; | |
} | |
memset(new_node, 0, sizeof(node)); | |
strcpy(new_node -> word, dictword); | |
//hashing the given word | |
int index = hash_function(dictword); | |
//adding the element to the linked list | |
new_node -> next = hashtable[index]; | |
hashtable[index] = new_node; | |
j++; | |
} | |
fclose(dict); | |
free(dictword); | |
return true; | |
} | |
// Returns number of words in dictionary if loaded else 0 if not yet loaded | |
unsigned int size(void) | |
{ | |
return j; | |
} | |
// Unloads dictionary from memory, returning true if successful else false | |
bool unload(void) | |
{ | |
// TODO | |
for (int l = 0; l < 15000; l++) | |
{ | |
node *fcursor = hashtable[l]; | |
while (fcursor != NULL) | |
{ | |
node *temp = fcursor; | |
fcursor = fcursor -> next; | |
free(temp); | |
} | |
} | |
return true; | |
} | |
//hashfunction | |
// Djb2 hashfunction source - http://www.cse.yorku.ca/~oz/hash.html | |
unsigned long hash_function(const char *word) | |
{ | |
unsigned long hash = 5381; | |
int c; | |
while ((c = *word++)) | |
{ | |
hash = ((hash << 5) + hash) + tolower(c); /* hash * 33 + c */ | |
} | |
unsigned long index = hash; | |
return index % 15000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment