Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <stdio.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <stdlib.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;
- // TODO: Choose number of buckets in hash table
- const unsigned int N = 80;
- // Hash table
- node *table[N];
- void free_list(struct node *n)
- {
- while(n != NULL)
- {
- node *tmp = n->next;
- free(n);
- n = tmp;
- }
- }
- bool insert_node(struct node *w)
- {
- unsigned int table_index = hash(w->word);
- if (table[table_index] == NULL)
- {
- table[table_index] = w;
- return true;
- }
- for (node *n = table[table_index]; n != NULL; n = n->next)
- {
- if (n->next == NULL)
- {
- n->next = w;
- return true;
- }
- }
- return false;
- }
- // Returns true if word is in dictionary, else false
- bool check(const char *word)
- {
- // TODO
- unsigned int word_hash = hash(word);
- for (node *n = table[word_hash]; n != NULL; n = n->next)
- {
- if (strcasecmp(n->word, word))
- {
- return true;
- }
- }
- return false;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- // TODO: Improve this hash function
- short ind = 0;
- unsigned int hash_val = 0;
- while (word[ind] != '\0')
- {
- if (isalpha(word[ind]))
- {
- hash_val += word[ind];
- }
- ind++;
- }
- return hash_val % N;
- // return tolower(word[0]) * tolower(word[1]) % N;
- }
- // Loads dictionary into memory, returning true if successful, else false
- bool load(const char *dictionary)
- {
- // TODO
- FILE *dict = fopen(dictionary, "r");
- if (dict == NULL)
- {
- printf("Could not open a dictionary file.\n");
- fclose(dict);
- return false;
- }
- node *word = malloc(sizeof(struct node));
- if (word != NULL)
- {
- printf("Starting loading dictionary\n");
- int char_index = 0;
- char c;
- while (fread(&c, sizeof(char), 1, dict))
- {
- if (isalpha(c) || (c == '\'' && char_index > 0))
- {
- word->word[char_index] = c;
- char_index++;
- }
- else if (c == '\n')
- {
- word->word[char_index + 1] = '\0';
- if (insert_node(word))
- {
- node *next_word = malloc(sizeof(struct node));
- if (next_word == NULL)
- {
- printf("Could not load next word.\n");
- fclose(dict);
- return false;
- }
- word->next = next_word;
- word = next_word;
- char_index = 0;
- }
- }
- }
- printf("LOADED!\n");
- fclose(dict);
- return true;
- }
- printf("Couldn't load dictionary\n");
- fclose(dict);
- return false;
- }
- // Returns number of words in dictionary if loaded, else 0 if not yet loaded
- unsigned int size(void)
- {
- // TODO
- printf("checking size\n");
- unsigned int elements = 0;
- for (int i = 0; i < N; i++)
- {
- for (node *j = table[i]; j != NULL; j = j->next)
- {
- elements++;
- }
- }
- printf("Returning size\n");
- return elements;
- }
- // Unloads dictionary from memory, returning true if successful, else false
- bool unload(void)
- {
- // TODO
- for (int i = 0; i < N; i++)
- {
- if (table[i] != NULL)
- {
- free_list(table[i]);
- }
- }
- return true;
- }
- /* void free_list(struct node *n)
- {
- if(n == NULL)
- {
- return;
- }
- free_list(n->next);
- free(n);
- return;
- } */
- /* void insert_node(struct node *w)
- {
- unsigned int table_index = hash(w->word);
- if (table[table_index] == NULL)
- {
- table[table_index] = w;
- return;
- }
- for (node *n = table[table_index]; n != NULL; n = n->next)
- {
- if (n->next == NULL)
- {
- n->next = w;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment