Guest User

Untitled

a guest
Apr 3rd, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <strings.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. #include "dictionary.h"
  9.  
  10. // Represents a node in a hash table
  11. typedef struct node
  12. {
  13.     char word[LENGTH + 1];
  14.     struct node *next;
  15. }
  16. node;
  17.  
  18. // Number of buckets in hash table
  19. const unsigned int N = 65335;
  20.  
  21. // Hash table
  22. node *table[N];
  23.  
  24. // declare words to count the words in the dictionary
  25. int dic_size = 0;
  26.  
  27. // Returns true if word is in dictionary else false
  28. bool check(const char *word)
  29. {
  30.     // set a temporary variable, to store the linked list
  31.     node *temp = table[hash(word)];
  32.  
  33.     // check if the word in the dictionary matches with the word in the hash table, case insensitive
  34.     if (strcasecmp(temp->word, word) == 0)
  35.     {
  36.         return true;
  37.     }
  38.  
  39.     while (temp -> next != NULL)
  40.     {
  41.         temp = temp -> next;
  42.  
  43.         if (strcasecmp(temp -> word, word) == 0)
  44.         {
  45.             return true;
  46.         }
  47.     }
  48.  
  49.  
  50.     return false;
  51. }
  52. // Hashes word to a number, using the djb2 algorithm
  53. unsigned int hash(const char *word)
  54. {
  55.     // set an integer named hash
  56.     unsigned int hash = 0;
  57.  
  58.     // iterate through the dictionary
  59.     for (int i = 0, n = strlen(word); i < n; i++)
  60.         hash = (hash << 2) ^ word[i];
  61.  
  62.     return hash % N;
  63. }
  64.  
  65. // Loads dictionary into memory, returning true if successful else false
  66. bool load(const char *dictionary)
  67. {
  68.  
  69.     // open up dictionary file
  70.     FILE *dictionary_file = fopen(dictionary, "r");
  71.  
  72.     char *dictword = malloc(LENGTH);
  73.  
  74.     // check to see if file is null
  75.     if (dictword == NULL)
  76.     {
  77.         // if so, exit the loop
  78.         return false;
  79.     }
  80.  
  81.     // read strings from file one at a time
  82.     while (fscanf(dictionary_file,"%s", dictword) != EOF)
  83.     {
  84.  
  85.         // create a new node for each word using malloc
  86.         node *n = malloc(sizeof(node));
  87.         n -> next = NULL;
  88.  
  89.         // check if malloc is null
  90.         if (n == NULL)
  91.         {
  92.             return false;
  93.         }
  94.  
  95.         // copy each word into a node using strcpy
  96.         strcpy(n -> word, dictword);
  97.  
  98.         // increment size variable
  99.         dic_size++;
  100.  
  101.         // set next to point at beginning of list
  102.         n -> next = table[hash(dictword)];
  103.  
  104.         // set array to point at n which becomes new beginning of the list
  105.         table[hash(dictword)] = n;
  106.  
  107.  
  108.     }
  109.     fclose(dictionary_file);
  110.     free(dictword);
  111.     return true;
  112. }
  113.  
  114. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  115. unsigned int size(void)
  116. {
  117.  
  118.     return dic_size;
  119.  
  120. }
  121.  
  122. // destroys all nodes. RECURSIVE FUNCTION!
  123. void destroy(node *head)
  124. {
  125.     if (head -> next != NULL)
  126.     {
  127.         destroy(head -> next);
  128.     }
  129.     free(head);
  130. }
  131.  
  132.  
  133. // frees all memory
  134. bool unload(void)
  135. {
  136.     for (int i = 0; i < N - 1; i++)
  137.     {
  138.         if (table[i] != NULL)
  139.         {
  140.             destroy(table[i]);
  141.         }
  142.     }
  143.     return true;
  144. }
Add Comment
Please, Sign In to add comment