Guest User

Untitled

a guest
Aug 19th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | Source Code | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <strings.h>
  9.  
  10. #include "dictionary.h"
  11.  
  12. // Represents a node in a hash table
  13. typedef struct node
  14. {
  15.     char word[LENGTH + 1];
  16.     struct node *next;
  17. } node;
  18.  
  19. // TODO: Choose number of buckets in hash table
  20. const unsigned int N = LENGTH;
  21.  
  22. // Hash table
  23. node *table[N];
  24.  
  25. // For size function
  26. int amount;
  27.  
  28. // Loads dictionary into memory, returning true if successful, else false
  29. bool load(const char *dictionary)
  30. {
  31.     char words[LENGTH + 1];
  32.     int counter = 0;
  33.  
  34.     // Open dictionary file
  35.     FILE *source = fopen(dictionary, "r");
  36.     if (source == NULL)
  37.     {
  38.         return false;
  39.     }
  40.  
  41.     // Read strings from file
  42.     while (fscanf(source, "%s", words) != EOF)
  43.     {
  44.         // Create a new node
  45.         node *n = malloc(sizeof(node));
  46.         if (n == NULL)
  47.         {
  48.             return false;
  49.         }
  50.  
  51.         // Fill in new node
  52.         strcpy(n->word, words);
  53.         n->next = NULL;
  54.  
  55.         // Find phrase bucket
  56.         int bucket = hash(words);
  57.  
  58.         table[bucket] = malloc(sizeof(node));
  59.         table[bucket]->next = NULL;
  60.  
  61.         // Insert node into hash table
  62.         if (counter == 0)
  63.         {
  64.             table[bucket]->next = n;
  65.         }
  66.         else
  67.         {
  68.             n->next = table[bucket]->next;
  69.             table[bucket]->next = n;
  70.         }
  71.         counter++;
  72.         amount++;
  73.     }
  74.  
  75.     // Close dictionary file
  76.     fclose(source);
  77.  
  78.     return true;
  79. }
  80.  
  81. // Hashes word to a number
  82. unsigned int hash(const char *word)
  83. {
  84.     // TODO: Improve this hash function
  85.     return strlen(word);
  86. }
  87.  
  88. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  89. unsigned int size(void)
  90. {
  91.     return amount;
  92. }
  93.  
  94. // Returns true if word is in dictionary, else false
  95. bool check(const char *word)
  96. {
  97.     // Create cursor
  98.     node *cursor = malloc(sizeof(node));
  99.     if (cursor == NULL)
  100.     {
  101.         return false;
  102.     }
  103.  
  104.     // Hash word for value
  105.     int value = hash(word);
  106.  
  107.     table[value] = malloc(sizeof(node));
  108.     table[value]->next = NULL;
  109.  
  110.     // Access linked list at index
  111.     cursor = table[value]->next;
  112.  
  113.     // Traverse linked list
  114.     while (cursor != NULL)
  115.     {
  116.         if (strcasecmp(cursor->word, word) == 0)
  117.         {
  118.             return true;
  119.         }
  120.         else
  121.         {
  122.             cursor = cursor->next;
  123.         }
  124.     }
  125.  
  126.     return false;
  127. }
  128.  
  129. // Unloads dictionary from memory, returning true if successful, else false
  130. bool unload(void)
  131. {
  132.     node *cursor = malloc(sizeof(node));
  133.     if (cursor == NULL)
  134.     {
  135.         return false;
  136.     }
  137.     node *tmp = malloc(sizeof(node));
  138.     if (tmp == NULL)
  139.     {
  140.         return false;
  141.     }
  142.  
  143.     for (int i = 0; i < LENGTH; i++)
  144.     {
  145.         table[i] = malloc(sizeof(node));
  146.         table[i]->next = NULL;
  147.  
  148.         cursor = table[i]->next;
  149.         tmp = cursor;
  150.         while (cursor != NULL)
  151.         {
  152.             cursor = cursor->next;
  153.             free(tmp);
  154.             tmp = cursor;
  155.         }
  156.     }
  157.     return true;
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment