Created
October 1, 2020 23:17
-
-
Save MonarchAB/c312c804b92c001777ff85d75c1ca550 to your computer and use it in GitHub Desktop.
Cs50 Speller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdbool.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <cs50.h> | |
| #include <string.h> | |
| #include <ctype.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; | |
| // Number of buckets in hash table | |
| const unsigned int N = 50; | |
| // Hash table | |
| node *table[N]; | |
| //Variable for wordcount | |
| int wordcount = 0; | |
| // Returns true if word is in dictionary else false | |
| bool check(const char *word) | |
| { | |
| unsigned int i = hash(word); | |
| node *cursor = table[i]; | |
| while (cursor->next != NULL) | |
| { | |
| if (strcasecmp(word, cursor->word) == 0) | |
| { | |
| return true; | |
| } | |
| else | |
| { | |
| cursor = cursor->next; | |
| } | |
| } | |
| return false; | |
| } | |
| // Hashes word to a number | |
| unsigned int hash(const char *word) | |
| { | |
| //Hash function sourced from https://www.reddit.com/r/cs50/comments/eo4zro/good_hash_function_for_speller/, modified djb2 function | |
| unsigned long hash = 5381; | |
| int c = *word; | |
| c = tolower(c); | |
| while (*word != 0) | |
| { | |
| hash = ((hash << 5) + hash) + c; | |
| c = *word++; | |
| c = tolower(c); | |
| } | |
| return hash % N; | |
| } | |
| // Loads dictionary into memory, returning true if successful else false | |
| bool load(const char *dictionary) | |
| { | |
| FILE *file = fopen(dictionary, "r"); | |
| if (file == NULL) | |
| { | |
| return false; | |
| } | |
| char temp [LENGTH + 1]; | |
| while(fscanf(file, "%s\n", temp) != EOF) | |
| { | |
| node *newword = malloc(sizeof(node)); | |
| if (newword == NULL) | |
| { | |
| return false; | |
| } | |
| strcpy(newword->word, temp); | |
| int storage = hash(temp); | |
| if (table[storage] != NULL) | |
| { | |
| newword->next = table[storage]; | |
| table[storage] = newword; | |
| } | |
| else | |
| { | |
| newword->next = NULL; | |
| table[storage] = newword; | |
| } | |
| wordcount++; | |
| } | |
| fclose(file); | |
| return true; | |
| } | |
| // Returns number of words in dictionary if loaded else 0 if not yet loaded | |
| unsigned int size(void) | |
| { | |
| return wordcount; | |
| } | |
| // Unloads dictionary from memory, returning true if successful else false | |
| bool unload(void) | |
| { | |
| node *cursor; | |
| node *tmp; | |
| int i = 0; | |
| for ( ; i < N; i++) | |
| { | |
| //Point cursor and temp to the start of the linked list | |
| cursor = table[i]; | |
| tmp = table[i]; | |
| while (cursor != NULL) | |
| { | |
| //Point cursor at next, free temp then point temp at cursor, and repeat until the pointer is pointing at null | |
| cursor = cursor->next; | |
| free(tmp); | |
| //Move along the linked list with tmp | |
| tmp = tmp->next; | |
| } | |
| } | |
| if (i == N && cursor == NULL) | |
| { | |
| return true; | |
| } | |
| else | |
| { | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line33 (cursor->next != NULL),the last word of the link list would not be check.
it should be (cursor != NULL).