Advertisement
Guest User

Untitled

a guest
Nov 15th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdbool.h>
  4.  
  5. #include "dictionary.h"
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <strings.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12. #include <cs50.h>
  13. #include <ctype.h>
  14.  
  15. // Represents a node in a hash table
  16. typedef struct node
  17. {
  18.     char word[LENGTH + 1];
  19.     struct node *next;
  20. }
  21. node;
  22.  
  23. // Number of buckets in hash table
  24. const unsigned int N = 1;
  25.  
  26. // Hash table
  27. node *table[N];
  28.  
  29. // Global Variable for word count incremented in load() called and returned in size()
  30. int nWordCount = 0;
  31.  
  32. // Returns true if word is in dictionary else false
  33. bool check(const char *word)
  34. {
  35.    
  36.     // TODO
  37.    
  38.     bool bFound = false;
  39.    
  40.     int key = hash(word);
  41.    
  42.     node *run1 = malloc(sizeof(node));
  43.    
  44.     if (run1 == NULL)
  45.     {
  46.         //printf("\n \t Unable to malloc check()");
  47.         return 1;
  48.     }
  49.    
  50.     //checks if the list is outright empty : meaning the hash did not map correctly
  51.     if (table[key]->next == NULL)
  52.     {
  53.         //printf("\n \t table[%i] is empty", key);
  54.         return 1;
  55.     }
  56.    
  57.     //if list is not empty
  58.     else
  59.     {
  60.         for(run1 = table[key]; run1->next != NULL; run1 = run1->next)
  61.         {
  62.             if (strcasecmp(word, run1->word))
  63.                 bFound = true;
  64.         }
  65.     }
  66.    
  67.     free(run1);
  68.     return bFound;
  69. }
  70.  
  71. // Hashes word to a number
  72. unsigned int hash(const char *word)
  73. {
  74.     // TODO
  75.     return 0;
  76. }
  77.  
  78. // Loads dictionary into memory, returning true if successful else false
  79. bool load(const char *dictionary)
  80. {
  81.     // TODO
  82.  
  83.  
  84.     int key = 0;
  85.     char word[LENGTH + 1];
  86.     bool bState = true;
  87.  
  88.  
  89.     //open file
  90.     FILE *fp1 = fopen(dictionary, "r");
  91.  
  92.     //check that the file was opened
  93.     if (fp1 == NULL)
  94.     {
  95.         //printf("\n \t Error opening file");
  96.         bState = false;
  97.         return 1;
  98.     }
  99.  
  100.     //reading each word in the file, while not EOF
  101.     while (fscanf(fp1, "%s", word) != EOF)
  102.     {
  103.         //creating the node n which will hold the word form the dictionary file
  104.         node *n = malloc(sizeof(node));
  105.  
  106.         //check if node was created
  107.         if (n == NULL)
  108.         {
  109.             //printf("\n \t Unable to malloc node n in load()");
  110.             bState = false;
  111.             return 1;
  112.         }
  113.  
  114.         //initialize values in node n
  115.         strcpy(n->word, word);
  116.         n->next = NULL;
  117.  
  118.         //counter for total words loaded
  119.         nWordCount++;
  120.  
  121.         //call hash function to get value of word
  122.         key = hash(word);
  123.  
  124.         //insert first node / head if array is empty
  125.         if (table[key] == NULL)
  126.         {
  127.             table[key] = n;
  128.         }
  129.  
  130.         //else array is not empty, insert new node at the end
  131.         else if (table[key] != NULL)
  132.         {
  133.             n->next = table[key];
  134.             table[key] = n;
  135.         }
  136.        
  137.         free(n);
  138.     }
  139.  
  140.     fclose(fp1);
  141.  
  142.     return bState;
  143. }
  144.  
  145. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  146. unsigned int size(void)
  147. {
  148.     // TODO
  149.     //return 0;
  150.  
  151.     return nWordCount;
  152. }
  153.  
  154. // Unloads dictionary from memory, returning true if successful else false
  155. bool unload(void)
  156. {
  157.     // TODO
  158.  
  159.     node *list = NULL;
  160.     bool bState = false;    
  161.    
  162.     while(list != NULL)
  163.     {
  164.         node *tmp = list->next;
  165.         free(list);
  166.         list = tmp;
  167.     }
  168.    
  169.     if(list == NULL)
  170.         bState = true;
  171.  
  172.     return bState;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement