Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <stdbool.h>
- #include "dictionary.h"
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <cs50.h>
- #include <ctype.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 = 1;
- // Hash table
- node *table[N];
- // Global Variable for word count incremented in load() called and returned in size()
- int nWordCount = 0;
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- // TODO
- bool bFound = false;
- int key = hash(word);
- node *run1 = malloc(sizeof(node));
- if (run1 == NULL)
- {
- //printf("\n \t Unable to malloc check()");
- return 1;
- }
- //checks if the list is outright empty : meaning the hash did not map correctly
- if (table[key]->next == NULL)
- {
- //printf("\n \t table[%i] is empty", key);
- return 1;
- }
- //if list is not empty
- else
- {
- for(run1 = table[key]; run1->next != NULL; run1 = run1->next)
- {
- if (strcasecmp(word, run1->word))
- bFound = true;
- }
- }
- free(run1);
- return bFound;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- // TODO
- return 0;
- }
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- // TODO
- int key = 0;
- char word[LENGTH + 1];
- bool bState = true;
- //open file
- FILE *fp1 = fopen(dictionary, "r");
- //check that the file was opened
- if (fp1 == NULL)
- {
- //printf("\n \t Error opening file");
- bState = false;
- return 1;
- }
- //reading each word in the file, while not EOF
- while (fscanf(fp1, "%s", word) != EOF)
- {
- //creating the node n which will hold the word form the dictionary file
- node *n = malloc(sizeof(node));
- //check if node was created
- if (n == NULL)
- {
- //printf("\n \t Unable to malloc node n in load()");
- bState = false;
- return 1;
- }
- //initialize values in node n
- strcpy(n->word, word);
- n->next = NULL;
- //counter for total words loaded
- nWordCount++;
- //call hash function to get value of word
- key = hash(word);
- //insert first node / head if array is empty
- if (table[key] == NULL)
- {
- table[key] = n;
- }
- //else array is not empty, insert new node at the end
- else if (table[key] != NULL)
- {
- n->next = table[key];
- table[key] = n;
- }
- free(n);
- }
- fclose(fp1);
- return bState;
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(void)
- {
- // TODO
- //return 0;
- return nWordCount;
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- // TODO
- node *list = NULL;
- bool bState = false;
- while(list != NULL)
- {
- node *tmp = list->next;
- free(list);
- list = tmp;
- }
- if(list == NULL)
- bState = true;
- return bState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement