Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <stdbool.h>
- #include <stdio.h>
- #include "dictionary.h"
- #include <stdlib.h>
- #include <string.h>
- #include <strings.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 = 55;
- // Hash table
- node *table[N];
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- int num = hash(word);
- node *cursor = malloc(sizeof(node));
- while (table[num] -> next != NULL)
- {
- cursor = table[num] -> next;
- if (strcasecmp(word, table[num] -> word) == 0)
- {
- return true;
- break;
- }
- else
- {
- table[num] = cursor;
- }
- }
- return true;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- unsigned int hash = 5381;
- int c;
- while ((c = *word++))
- {
- hash = ((hash << 5) + hash) + tolower(c);
- }
- return hash % N;
- }
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- int size1 = 0;
- FILE *file = fopen (dictionary, "r");
- if(file == NULL) {
- printf("File could not be opened.\n");
- return false;
- }
- char word1[46];
- while(fscanf(file, "%s" ,word1) != EOF)
- {
- node *n = malloc(sizeof(node));
- if (n == NULL)
- {
- unload();
- return 1;
- }
- strcpy(n -> word, word1);
- n -> next = NULL;
- size1 = size(size1);
- int index = hash(word1);
- if (table[index] == NULL)
- {
- table[index] = n;
- }
- else
- {
- n -> next = table[index];
- table[index] = n;
- }
- }
- fclose(file);
- return true;
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(int size1)
- {
- return size1+1;
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- for (int i = 0; i < N; i++)
- {
- node *tmp = table[i];
- node *cursor = table[i];
- while(cursor -> next != NULL)
- {
- cursor = cursor -> next;
- free(tmp);
- tmp = cursor;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement