Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <string.h>
- #include "dictionary.h"
- #define NUM_BUCKETS 675
- // Node/linked list for hash table
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node *next;
- } node;
- // Represents hash table
- typedef struct
- {
- node *next[NUM_BUCKETS];
- } hash_table;
- hash_table *table;
- unsigned int word_count = 0;
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- // A simple hash function based on the first two characters of the word
- int b = 26 * (toupper(word[0]) - 'A') + toupper(word[1]) - 'A';
- return b % NUM_BUCKETS;
- }
- // Loads dictionary into memory, returning true if successful, else false
- bool load(const char *dictionary)
- {
- // Allocate memory for the hash table
- table = malloc(sizeof(hash_table));
- if (table == NULL)
- {
- return false;
- }
- // Initialize hash table and linked lists to NULL
- for (int i = 0; i < NUM_BUCKETS; i++)
- {
- table->next[i] = NULL;
- }
- // Open dictionary file
- FILE *file = fopen(dictionary, "r");
- if (file == NULL)
- {
- return false;
- }
- char word[LENGTH + 1];
- // Load words from the dictionary into the hash table
- while (fscanf(file, "%s", word) != EOF)
- {
- // Create a new node for each word
- node *new_node = malloc(sizeof(node));
- if (new_node == NULL)
- {
- fclose(file);
- return false;
- }
- // Copy the word into the node
- strcpy(new_node->word, word);
- // Hash the word to get the index in the hash table
- int index = hash(word);
- // Insert the node at the beginning of the linked list
- new_node->next = table->next[index];
- table->next[index] = new_node;
- // Increment word count
- word_count++;
- }
- // Close the file
- fclose(file);
- return true;
- }
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- // Convert the word to lowercase for case-insensitive comparison
- char lowercase_word[LENGTH + 1];
- for (int i = 0; word[i] != '\0'; i++)
- {
- lowercase_word[i] = tolower(word[i]);
- }
- lowercase_word[strlen(word)] = '\0';
- // Hash the word to get the index in the hash table
- int index = hash(lowercase_word);
- // Traverse the linked list at the given index
- node *cursor = table->next[index];
- while (cursor != NULL)
- {
- // Compare the word with the node's word in the linked list
- if (strcmp(cursor->word, lowercase_word) == 0)
- {
- return true;
- }
- cursor = cursor->next;
- }
- return false;
- }
- // Returns number of words in dictionary if loaded, else 0 if not yet loaded
- unsigned int size(void)
- {
- return word_count;
- }
- // Unloads dictionary from memory, returning true if successful, else false
- bool unload(void)
- {
- for (int i = 0; i < NUM_BUCKETS; i++)
- {
- // Traverse the linked list at each index and free memory for each node
- node *cursor = table->next[i];
- while (cursor != NULL)
- {
- node *temp = cursor;
- cursor = cursor->next;
- free(temp);
- }
- }
- // Free memory for the hash table
- free(table);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement