summaryrefslogtreecommitdiff
path: root/wk5/pset/speller/dictionary.c
diff options
context:
space:
mode:
authorFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-04-29 03:08:22 +0000
committerFudgerboy <91767657+Fudgerboy@users.noreply.github.com>2024-04-29 03:08:22 +0000
commit5bfc7e361ad922f2290985387dbec6a37ab00ced (patch)
tree40e91ae102ff13e9e37ae5c7c438531e4f6194ae /wk5/pset/speller/dictionary.c
parent94026cf1d5bfe86b04e9b139f9fc18949b00898c (diff)
Sun, Apr 28, 2024, 8:08 PM -07:00
Diffstat (limited to 'wk5/pset/speller/dictionary.c')
-rw-r--r--wk5/pset/speller/dictionary.c57
1 files changed, 55 insertions, 2 deletions
diff --git a/wk5/pset/speller/dictionary.c b/wk5/pset/speller/dictionary.c
index a20c467..e984b9f 100644
--- a/wk5/pset/speller/dictionary.c
+++ b/wk5/pset/speller/dictionary.c
@@ -35,8 +35,61 @@ unsigned int hash(const char *word)
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
- // TODO
- return false;
+ // initialize the table
+ for (int i = 0; i < N; i++) {
+ // malloc size of next
+ table[i] = NULL;
+ }
+
+ // Open dictionary file
+ FILE *source = fopen(dictionary, "r");
+ if (source == NULL)
+ {
+ return false;
+ }
+
+ //read each word in dictionary
+ char word[LENGTH + 1];
+ // use fscanf(file, "%s", word) to grab words
+ // check for ended file
+ while(fscanf(source, "%s", word) == 1)
+ {
+ // update size int
+ siz++;
+ // create new node
+ // use malloc
+ node *ptr = malloc(sizeof(node));
+ // check if return is NULL
+ if (ptr == NULL)
+ {
+ fclose(source);
+ return false;
+ }
+ // copy word from fscanf into node using strcpy
+ strcpy(ptr->word, word);
+ // set the new ptr
+ ptr->next = NULL;
+ // hash the word to find the bucket it goes in
+ int val = hash(ptr->word);
+ // put new node at begining of bucket
+ if (table[val] == NULL)
+ {
+ // if empty put it there
+ table[val] = ptr;
+ }
+ else
+ {
+ // if not empty move the current first one down,
+ // then put the new one there
+ ptr->next = table[val];
+ table[val] = ptr;
+ }
+
+ }
+
+ // Close the dictionary file
+ fclose(source);
+ return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded