Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements a dictionary's functionality.
- */
- #include <ctype.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "dictionary.h"
- #define ALPHLEN 26 // length of alphabet
- // define trie struct
- typedef struct trie
- {
- bool isWord;
- struct trie *alph[ALPHLEN+1];
- } trie;
- // function declarations
- void insert(char *word);
- bool makeEmpty(trie *node);
- // global variables
- int dSize = 0;
- trie *root = NULL;
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char *word)
- {
- // create traversal pointer
- trie *ptr = root;
- // iterate through characters of word
- for (int i = 0; word[i] != '\0'; i++)
- {
- // store character
- char c = word[i];
- // assigns index of 0-25 for a-z, 26 for apostrophe
- int index = (c == '\'') ? ALPHLEN : toupper(c) - 65;
- // if index cointains null pointer, return failure
- if (ptr->alph[index] == NULL)
- {
- return false;
- }
- // advance pointer
- ptr = ptr->alph[index];
- }
- // check if word
- if (ptr->isWord)
- {
- return true;
- }
- return false;
- }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- // open dictionary file
- FILE *d = fopen(dictionary, "r");
- // check that file opened correctly
- if (d == NULL)
- {
- return false;
- }
- //initialize trie
- root = malloc(sizeof(trie));
- root->isWord = false;
- // variable for storing strings
- char word[LENGTH];
- // read string until end of file
- while (fgets(word, LENGTH+2, d))
- {
- insert(word);
- dSize++;
- }
- // close dictionary file
- fclose(d);
- // return success
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- return dSize;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- makeEmpty(root);
- return true;
- }
- // utility functions
- // insert one word into trie
- void insert(char *word)
- {
- // create traversal pointer
- trie *ptr = root;
- // iterate through characters of word
- for(int i = 0; word[i] != '\n'; i++)
- {
- // store character
- char c = word[i];
- // assigns index of 0-25 for a-z, 26 for apostrophe
- int index = (c == '\'') ? ALPHLEN : toupper(c) - 65;
- // if index contains null pointer, create new trie
- if (ptr->alph[index] == NULL)
- {
- ptr->alph[index] = malloc(sizeof(trie));
- ptr->alph[index]->isWord = false;
- }
- // advance pointer
- ptr = ptr->alph[index];
- }
- // set bool to true at end of word
- ptr->isWord = true;
- }
- // frees a node and all children
- bool makeEmpty(trie *node)
- {
- // iterate through array in trie
- for (int i = 0; i < ALPHLEN; i++)
- {
- if (node->alph[i] != NULL)
- {
- makeEmpty(node->alph[i]);
- }
- }
- // if all pointers in array are null, free it
- free(node);
- //return success
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement