Created
April 11, 2019 17:25
-
-
Save Praful932/12f6575328df465ee06389b138b8e382 to your computer and use it in GitHub Desktop.
This file contains 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 <ctype.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <strings.h> | |
#include <stdlib.h> | |
#include "dictionary.h" | |
// Represents number of buckets in a hash table | |
#define N 26 | |
// Represents a node in a hash table | |
typedef struct node | |
{ | |
char word[LENGTH + 1]; | |
struct node *next; | |
} | |
node; | |
// Represents a hash table | |
node *hashtable[N]; | |
// word count | |
int wordcount=0; | |
bool loaded=false; | |
// Hashes word to a number between 0 and 25, inclusive, based on its first letter | |
unsigned int hash(const char *word) | |
{ | |
return tolower(word[0]) - 'a'; | |
} | |
// Loads dictionary into memory, returning true if successful else false | |
bool load(const char *dictionary) | |
{ | |
node *head; | |
// Initialize hash table | |
for (int i = 0; i < N; i++) | |
{ | |
hashtable[i] = NULL; | |
} | |
// Open dictionary | |
FILE *file = fopen(dictionary, "r"); | |
if (file == NULL) | |
{ | |
unload(); | |
return false; | |
} | |
// Buffer for a word | |
char word[LENGTH + 1]; | |
// Insert words into hash table | |
while (fscanf(file, "%s", word) != EOF) | |
{ | |
int h=hash(word); | |
head=(node *)malloc(sizeof(node)); | |
if(head==NULL) | |
{ | |
unload(); | |
return false; | |
} | |
// intially when hashtable[h] contains NULL | |
if(hashtable[h]==NULL) | |
{ | |
strcpy(head->word,word); | |
hashtable[h]=head; | |
head->next=NULL; | |
wordcount++; | |
} | |
// for adding a node to hashtable[h] when already first word is added | |
else | |
{ | |
// head=(node *)malloc(sizeof(node)); | |
strcpy(head->word,word); | |
head->next=hashtable[h]; | |
hashtable[h]=head; | |
wordcount++; | |
} | |
} | |
// Close dictionary | |
free(head); | |
fclose(file); | |
// Indicate success | |
loaded=true; | |
return true; | |
} | |
// Returns number of words in dictionary if loaded else 0 if not yet loaded | |
unsigned int size(void) | |
{ | |
if(loaded==true) | |
return wordcount; | |
return 0; | |
} | |
// Returns true if word is in dictionary else false | |
bool check(const char *word) | |
{ | |
node *cursor=hashtable[hash(word)]; | |
while(cursor != NULL) | |
{ | |
if(strcasecmp(cursor->word,word)==0) | |
{ | |
return true; | |
} | |
else | |
cursor=cursor->next; | |
} | |
return false; | |
} | |
// Unloads dictionary from memory, returning true if successful else false | |
bool unload(void) | |
{ | |
for(int i=0 ; i < N ; 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