Guest User

speller

a guest
Jul 26th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <strings.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "dictionary.h"
  10.  
  11. // Represents a node in a hash table
  12. typedef struct node
  13. {
  14.     char word[LENGTH + 1];
  15.     struct node *next;
  16. }
  17. node;
  18.  
  19. // TODO: Choose number of buckets in hash table
  20. const unsigned int N = 26;
  21.  
  22. // Hash table
  23. node *table[N];
  24.  
  25. int size_check = 0;
  26.  
  27. // Returns true if word is in dictionary, else false
  28. bool check(const char *word)
  29. {
  30.     // TODO
  31.     int index = hash(word);
  32.     node *cursor = table[index];
  33.     while (cursor != NULL)
  34.     {
  35.         if (strcasecmp(cursor->word, word) == 0)
  36.         {
  37.             return true;
  38.         }
  39.         cursor = cursor->next;
  40.     }
  41.     return false;
  42. }
  43.  
  44. // Hashes word to a number
  45. unsigned int hash(const char *word)
  46. {
  47.     // TODO: Improve this hash function
  48.     return toupper(word[0]) - 'A';
  49. }
  50.  
  51. // Loads dictionary into memory, returning true if successful, else false
  52. bool load(const char *dictionary)
  53. {
  54.     // TODO
  55.     FILE *file = fopen(dictionary, "r");
  56.     if (file == NULL)
  57.     {
  58.         return false;
  59.     }
  60.  
  61.     char word[LENGTH + 1];
  62.  
  63.     while (fscanf(file, "%s", word) != EOF)
  64.     {
  65.         node *temp = malloc(sizeof(node));
  66.         if (temp == NULL)
  67.         {
  68.             unload();
  69.             return false;
  70.         }
  71.  
  72.         strcpy(temp->word, word);
  73.         temp->next = NULL;
  74.  
  75.         size_check++;
  76.  
  77.         int index = hash(temp->word);
  78.         node *head = malloc(sizeof(node));
  79.  
  80.         if (table[index] == NULL)
  81.         {
  82.             head = temp;
  83.             table[index] = head;
  84.         }
  85.         else
  86.         {
  87.             temp->next = head;
  88.             head = temp;
  89.         }
  90.     }
  91.     fclose(file);
  92.     return true;
  93. }
  94.  
  95. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  96. unsigned int size(void)
  97. {
  98.     // TODO
  99.     return size_check;
  100. }
  101.  
  102. // Unloads dictionary from memory, returning true if successful, else false
  103. bool unload(void)
  104. {
  105.     // TODO
  106.     for (int i = 0; i < N; i++)    // Check all "buckets"
  107.     {
  108.         node *cursor = table[i];
  109.         while (cursor != NULL)     // Check the list until end
  110.         {
  111.             node *tmp = table[i];
  112.             cursor = cursor->next;
  113.             free(tmp);
  114.             tmp = cursor;
  115.         }
  116.     }
  117.     return true;
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment