Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implements a dictionary's functionality
- #include <stdio.h>
- #include <strings.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <cs50.h>
- #include "dictionary.h"
- #include <ctype.h>
- // Represents a node in a hash table
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node *next;
- }
- node;
- //create a hash table, an array of hash values
- int N = 26;
- int count = 0;
- //node *table[26*3];
- node *table[26];
- bool check(const char *word)
- {
- node *cursor = table[hash(word)];
- while (cursor != NULL)
- {
- if (strcasecmp(cursor->word,word) == 0)
- {
- return true;
- }
- cursor = cursor->next;
- }
- return false;
- }
- unsigned int hash(const char *word)
- {
- unsigned int val = 0;
- while(*word)// a pointer to the first char in a pointer to chars called word
- {
- val += tolower(*word++); //increment the pointer to the
- }
- return val%N;
- //return 0;
- }
- bool load(const char *dictionary)
- {
- char word[LENGTH + 1]; // an array of characters
- FILE *dictr = fopen(dictionary,"r"); // a pointer to a file in read mode
- if (dictr == NULL) // if there's nothing there
- {
- return false;
- }
- char buffer[LENGTH+1];
- while (fscanf(dictr,"%s", buffer) == 1)
- {
- node *myword = malloc(sizeof(node));
- if (myword == NULL)
- {
- return false;
- }
- strcpy(myword->word,buffer);
- int index = hash(buffer);
- myword->next = table[index];
- table[index] = myword;
- count++;
- }
- return true;
- }
- unsigned int size(void)
- {
- return count;
- }
- bool unload(void)
- {
- while (cursor != NULL)
- {
- node *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment