Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <stdio.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <strings.h>
- #include <string.h>
- #include <stdlib.h>
- #include "dictionary.h"
- // Represents a node in a hash table
- typedef struct node
- {
- char word[LENGTH + 1]; // (*n).word[]
- struct node *next;
- }
- node;
- // Choose number of buckets in hash table
- const unsigned int N = 100000;
- // Hash table
- node *table[N];
- // Count words
- int count = 0;
- // Returns true if word is in dictionary, else false
- bool check(const char *word)
- {
- // Gets hash value of word
- int n = hash(word);
- // Iterates through linked list
- for (node *tmp = table[n]; tmp != NULL; tmp = tmp->next)
- {
- // Compares string case-insensitive
- if (strcasecmp(tmp->word, word) == 0)
- {
- // If word is in dictionary, return true and end loop
- return true;
- break;
- }
- }
- // Else return false
- return false;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- // Initialise an int for length of the string and n
- int length = 0;
- int n = 0;
- // Count number of characters in the word
- for (int i = 0; word[i] != '\0'; ++i)
- {
- // Adds one to length for each character
- length++;
- }
- // For each character in the word
- for (int j = 0; j < length; j++)
- {
- // Adds character's ASCII value to the count n
- n += tolower(word[j]);
- }
- // Returns remainder of n divided by N such that return value never greater than N
- return (n % N);
- }
- // Loads dictionary into memory, returning true if successful, else false
- bool load(const char *dictionary)
- {
- // Open file of dictionary
- FILE *dict = fopen(dictionary, "r");
- // If dictionary is not present
- if (dictionary == NULL)
- {
- printf("Unable to open dictionary");
- return false;
- }
- // Char array to store each word going to be added
- char buffer[LENGTH + 1];
- // Read strings from file
- while (fscanf(dict, "%s", buffer) != EOF)
- {
- // Allocate memory for each word and node
- node *n = malloc(sizeof(node));
- if (n == NULL)
- {
- return false;
- }
- // Copy word into node
- strcpy(n->word, buffer);
- // Hash the word to get a hash value for the word
- unsigned int u = hash(buffer);
- // If head does not point to anything
- if (table[u] == NULL)
- {
- // Set n to be the first element
- table[u] = n;
- }
- else
- {
- // Set n to point to first element
- n->next = table[u];
- // Set head to point to n, making it the first element of linked list and n->next becomes second element
- table[u] = n;
- }
- count++;
- }
- // Close file of dictionary
- fclose(dict);
- return true;
- }
- // Returns number of words in dictionary if loaded, else 0 if not yet loaded
- unsigned int size(void)
- {
- // Returns the global variable count
- return count;
- }
- // Unloads dictionary from memory, returning true if successful, else false
- bool unload(void)
- {
- // Iterate through each bucket in hash table
- for (int i = 0; i < N; i++)
- {
- // Create new node called cursor that is equal to table[i]
- node *cursor = table[i];
- // While the cursor is pointing at something
- while (cursor != NULL)
- {
- // Set node tmp to what the cursor is pointing at
- node *tmp = cursor;
- // Set cursor to the next element in linked list
- cursor = cursor->next;
- // Free tmp
- free(tmp);
- }
- // If cursor pointing at nothing and i reaches last bucket, dictionary successfully unloaded and return true
- if ((cursor == NULL) && (i == N - 1))
- {
- return true;
- }
- }
- // Else return false
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement