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 = 26;
- // 26 for 26 letters in alphabet
- //global variable for word count used in load() and size()
- int nWordCount = 0;
- // Hash table
- node *table[N];
- //initializing empty and NULL values to table
- for(int i = 0; i < N; i++)
- {
- node *n = malloc(sizeof(node));
- if (n == NULL)
- {
- printf("\n \t Unable to malloc.");
- return 1;
- }
- strcpy(table[i]->word, "");
- table[i]->next = n;
- table[i]->next = NULL;
- }
- // Returns true if word is in dictionary else false
- bool check(const char *word)
- {
- // TODO
- //case insensitive
- // 1) hash word using has function
- // 2) traverse linked list at has index , use strcasecmp()
- /* Start with cursor set to first item in linked list.
- keep moving cursor until you get to NULL, checking each node for the word.
- */
- bool bFound = false;
- int key = hash(word);
- node *temp = malloc(sizeof(node));
- temp = NULL;
- //checks if the list is outright empty == NULL
- if(table[key]->next == NULL)
- {
- bFound = false;
- }
- //if list is not emtpy
- else
- {
- for (temp = table[key]; temp->next != NULL; temp = temp->next)
- {
- if (strcasecmp(word, temp->word))
- {
- bFound = true;
- }
- }
- }
- free(temp);
- return bFound;
- }
- // Hashes word to a number
- unsigned int hash(const char *word)
- {
- // TODO
- //hash by first letter
- int val = 0;
- char c = tolower(word[0]);
- switch(c)
- {
- case ('a'):
- val = 0;
- break;
- case ('b'):
- val = 1;
- break;
- case ('c'):
- val = 2;
- break;
- case ('d'):
- val = 3;
- break;
- case ('e'):
- val = 4;
- break;
- case ('f'):
- val = 5;
- break;
- case ('g'):
- val = 6;
- break;
- case ('h'):
- val = 7;
- break;
- case ('i'):
- val = 8;
- break;
- case ('j'):
- val = 9;
- break;
- case ('k'):
- val = 10;
- break;
- case ('l'):
- val = 11;
- break;
- case ('m'):
- val = 12;
- break;
- case ('n'):
- val = 13;
- break;
- case ('o'):
- val = 14;
- break;
- case ('p'):
- val = 15;
- break;
- case ('q'):
- val = 16;
- break;
- case ('r'):
- val = 17;
- break;
- case ('s'):
- val = 18;
- break;
- case ('t'):
- val = 19;
- break;
- case ('u'):
- val = 20;
- break;
- case ('v'):
- val = 21;
- break;
- case ('w'):
- val = 22;
- break;
- case ('x'):
- val = 23;
- break;
- case ('y'):
- val = 24;
- break;
- case ('z'):
- val = 25;
- break;
- default:
- printf("Invalid operation");
- }
- return val;
- }
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- int key = 0;
- char word[45];
- node *cursor = malloc(sizeof(node));
- cursor = NULL;
- // TODO
- //open file
- FILE *fp1 = fopen(dictionary, "r");
- //check if file is NULL
- if (fp1 == NULL)
- {
- printf ("/n /t Error opening file!");
- return(1);
- }
- while (fscanf(fp1, "%s", word) != EOF)
- {
- //creating the node
- node *n = malloc(sizeof(node));
- strcpy (n->word, word);
- nWordCount++;
- n->next = NULL;
- //call hash function to get hash value of word
- key = hash(word);
- //inserts first node / head if array is empty
- if (table[key] == NULL)
- {
- table[key]->next = n;
- }
- //else array is not empty, inserts new node at the end
- else if (table[key] != NULL)
- {
- //goes to index in table
- cursor->next = table[(N-1)];
- //traverses linked list to get to the last node
- while(cursor->next != NULL)
- {
- cursor->next = cursor->next->next;
- }
- //adds new node to the end
- cursor->next = n;
- }
- }
- fclose(fp1);
- /* 1) Open dictionary file
- use fopen, check if return value is NULL */
- /* 2) Read strings from file one at a time
- fscanf(file, "%s", word), EOF at End of File */
- /* 3) Create a new word for each node
- use malloc, check if return value is NULL, copy word into node using strcpy */
- // 4) Hash word to obtain hash value
- // 5) Insert node into hash table at that location
- //return false;
- return true;
- }
- // Returns number of words in dictionary if loaded else 0 if not yet loaded
- unsigned int size(void)
- {
- // TODO
- //return number of words in dictionary
- return nWordCount;
- //return 0;
- }
- // Unloads dictionary from memory, returning true if successful else false
- bool unload(void)
- {
- // TODO
- // free all nodes in the linked lists
- // runner + trailer. Runner keeps moving forward, trailer is the one freeing the node.
- node *runner = malloc(sizeof(node));
- runner = NULL;
- node *trailer = malloc(sizeof(node));
- trailer = NULL;
- for (int i = 0; i < N; i++)
- {
- runner = table[i];
- trailer = table[i];
- do
- {
- //move runner 1 node forward
- runner->next = table[i]->next;
- //move trailer 1 node forward, same position as runner
- trailer->next = runner;
- //move runner 1 node forward, now 1 node ahead of trailer
- runner->next = runner->next->next;
- free(trailer);
- trailer->next = runner;
- }while (runner->next != NULL);
- free(runner);
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement