// Implements a dictionary's functionality #include #include #include #include #include #include #include #include "dictionary.h" // define hashtable size of 2 ^ 16 #define HASHTABLE_SIZE 65536 // declare a counter variable to count number of words in the dictionary int wordcount = 0; // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node *next; } node; // Number of buckets in hash table const unsigned int N = 65536; // declare the hashtable node *hashtable[HASHTABLE_SIZE]; // Hash table node *table[HASHTABLE_SIZE]; // Returns true if word is in dictionary else false bool check(const char *word) { // TODO // declare lowercase word char lcase_word[LENGTH + 1]; // convert words to lowercase for (int i = 0; i < LENGTH; i++) { lcase_word[i] = tolower(word[i]); } // hash word to obtain hash value int index = hash(lcase_word); // set up cursor variable to traverse linked list node *cursor = hashtable[index]; // have cursor traverse linked listed until end while (cursor != NULL) { // check words in node to find if word if target word is there if (strcasecmp (word, cursor->word) == 0) { // return true if target word is there return true; } cursor = cursor->next; } return false; } // Hashes word to a number unsigned int hash(const char *word) { // TODO // hash function from: https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/cf9nlkn/ unsigned int hash = 0; for (int i = 0, n = strlen(word); i < n; i++) { hash = (hash << 2) ^ word[i]; } return hash % HASHTABLE_SIZE; } // Loads dictionary into memory, returning true if successful else false bool load(const char *dictionary) { // TODO // open dictionary file FILE *dictptr = fopen(dictionary, "r"); if (dictptr == NULL) { return false; } // declare variable to read words into char word[LENGTH + 1]; while (fscanf(dictptr, "%s", word) != EOF) { // allocate enough memory to store a node node *newnode = malloc(sizeof(node)); // check if malloc was successful if (newnode == NULL) { return false; } // copy words into node strcpy (newnode->word, word); newnode->next = NULL; // hash the word: insert into hashtable int index = hash(newnode->word); // if bucket is empty if (hashtable[index] == NULL) { hashtable[index] = newnode; newnode->next = NULL; } else { // if bucket is not empty newnode->next = hashtable[index]; hashtable[index] = newnode; } wordcount++; } fclose(dictptr); return true; } // Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { // TODO // from load function word added from dictionary were counted as each was added return wordcount; } // Unloads dictionary from memory, returning true if successful else false bool unload(void) { // TODO // iterate over all the nodes in the linked list for (int i = 0; i < HASHTABLE_SIZE; i++) { // initialise a tmp variable and a cursor varible // point them to beginning of the linked list node *tmp = hashtable[i]; node *cursor = hashtable[i]; while (hashtable[i] != NULL) { if (cursor != NULL) { tmp = cursor; cursor = cursor->next; free(tmp); } } free(cursor); } return true; }