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"
- // Represents a node in a hash table
- typedef struct node
- {
- char word[LENGTH + 1];
- struct node *next;
- }
- node;
- const unsigned int N = 26;
- //create a hash table, an array of hash values
- node *table[N];
- bool check(const char *word)
- {
- //int hval = hash(word);
- //node *cursor = table[hval];
- node *cursor = table[hash(word)];
- while (cursor != NULL)
- {
- if (strcasecmp(cursor->word, word) == false)
- {
- cursor = cursor->next;
- }
- else
- {
- return true;
- }
- }
- //char *cursor = word[0];
- // TODO
- return false;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)// to hash a-z 0-26, maybe use ascii value and subtract?
- {
- int val = 0;
- //int cval;
- for (int i = 0; word[i] != '\0'; i++)
- {
- //val += word[i] - 97;
- //val += word[i] - 13; ... 13 is the value of '
- val += word[i];
- }
- // TODO
- return val%26;
- // maybe try a hash table that will use the first 3 letters
- //return 0;
- }
- //char *name = "jacob";
- //int h = hash(name);
- //printf("name prints %s and the hash value of name is: %i\n",name,h);
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- // TODO
- //node *table[N];
- node *n = malloc(sizeof(node));
- char word[LENGTH + 1];
- FILE *dictr = fopen(dictionary,"r");
- //FILE *dictr = fopen("dictionaries/large","r");
- //FILE *dictr = fopen(argv[1],"r");
- // open the file
- if (dictr == NULL)
- {
- return false;
- }
- while (fscanf(dictr,"%s", word) != EOF)//read strings one at a time
- {
- fscanf(dictr,"%s",word);
- //printf("%s",word);
- }
- //fread(hash(dictr),sizeof(char),1,dictr);
- // malloc?
- // int *dictr = malloc(3*sizeof(int));
- if (dictr != NULL)
- {
- return true;
- }
- else
- return false;
- char wordnow[LENGTH+1];
- node *myword = NULL;
- while (fscanf(dictr,"%s\n", wordnow) != EOF)
- {
- myword = malloc(sizeof(node));
- if (myword == NULL)
- {
- return false;
- }
- //strcpy((*myword).word, wordnow);
- strcpy(myword->word, wordnow);// copy wordnow to the word field of the myword node
- //(*myword).next = NULL;
- }
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(void)
- {
- // TODO
- int count = 0;
- //for (int i = 0; table[i] != NULL;i++)
- int i = 0;
- //while (table[i] != NULL)
- while (table[i]->next != NULL)
- {
- count++;
- i++;
- }
- if (count == 0)
- {
- return 0;
- }
- else
- {
- return count;
- }
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- //int i = 0;
- node *cursor = table[hash(0)];
- //node *tmp = table[hash(i)];
- //node *tmp = cursor;?
- //while (table[i]->next != NULL)
- while (cursor->next != NULL)
- {
- node *tmp = cursor;
- cursor = cursor->next;
- free(tmp);
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment