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 <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include "dictionary.h"
- // Represents a node in a hash table
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node *next;
- }
- node;
- // TODO: Choose number of buckets in hash table
- const unsigned int N = 1125;
- // Hash table
- node *table[N];
- //Stores whether or not the dictionary was loaded
- bool loaded = false;
- //Stores number of words in dictionary
- unsigned int num_words = 0;
- // Returns true if word is in dictionary, else false
- bool check(const char *word)
- {
- //Hash word to be checked
- char *copy = malloc(strlen(word) + 1);
- strcpy(copy, word);
- for (int i = 0; i < strlen(copy); i++)
- {
- copy[i] = tolower(copy[i]);
- }
- unsigned int hash_val = hash(copy);
- free(copy);
- //Allocate memory for a node pointer to index through the hash table and point
- //to the appropriate hash table head
- node *index = malloc(sizeof(node));
- index = table[hash_val];
- //Index through list until matching word is found
- while (index != NULL)
- {
- if(!strcasecmp(index->word, word))
- {
- index = NULL;
- free(index);
- return true;
- }
- index = index->next;
- }
- index = NULL;
- free(index);
- return false;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- //Hash word to lowercase ASCII - 'a' per letter
- unsigned int hash_val = 0;
- for(int i = 0; i < strlen(word); i++)
- {
- if(word[i] == '\'')
- {
- hash_val += 1;
- }
- if(tolower(word[i]) >= 'a' && tolower(word[i]) <= 'z')
- {
- hash_val += (tolower(word[i]) - 'a');
- }
- }
- return hash_val;
- }
- // Loads dictionary into memory, returning true if successful, else false
- bool load(const char *dictionary)
- {
- //Open dictionary
- FILE *file = fopen(dictionary, "r");
- //Check that dictionary opened correctly/exists
- if (file == NULL)
- {
- return false;
- }
- unsigned int hash_val;
- //Load the words from the dictionary into the hash table
- char *buffer = malloc((sizeof(char) * LENGTH) + 1);
- while (fscanf(file, "%s", buffer) != EOF)
- {
- //Allocate space for new node
- node *n = malloc(sizeof(node));
- if (n == NULL)
- {
- unload();
- free(n);
- free(buffer);
- return false;
- }
- //Copy new word into new node
- strcpy(n->word, buffer);
- n->next = NULL;
- //Hash new word
- hash_val = hash(n->word);
- //Link new word into hash table
- n->next = table[hash_val];
- table[hash_val] = n;
- num_words++;
- }
- fclose(file);
- free(buffer);
- loaded = true;
- return true;
- }
- // Returns number of words in dictionary if loaded, else 0 if not yet loaded
- unsigned int size(void)
- {
- if (loaded)
- {
- return num_words;
- }
- else
- {
- return 0;
- }
- }
- // Unloads dictionary from memory, returning true if successful, else false
- bool unload(void)
- {
- //Allocate memory for a cursor and temp pointer
- node *cursor = malloc(sizeof(node));
- node *tmp = malloc(sizeof(node));
- //Variable to count how many items were freed
- int count = 0;
- //Itterate through hash table to free dictionary
- for (int i = 0; i < N; i++)
- {
- //Change table head
- cursor = table[i];
- while (cursor != NULL)
- {
- ////Itterate through linked list
- tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- count++;
- }
- }
- free(cursor);
- if(count == num_words)
- {
- return true;
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment