Advertisement
Guest User

Untitled

a guest
Dec 12th, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <strings.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #include "dictionary.h"
  10.  
  11. // Represents a node in a hash table
  12. typedef struct node
  13. {
  14. char word[LENGTH + 1]; // (*n).word[]
  15. struct node *next;
  16. }
  17. node;
  18.  
  19. // Choose number of buckets in hash table
  20. const unsigned int N = 100000;
  21.  
  22. // Hash table
  23. node *table[N];
  24.  
  25. // Count words
  26. int count = 0;
  27.  
  28. // Returns true if word is in dictionary, else false
  29. bool check(const char *word)
  30. {
  31. // Gets hash value of word
  32. int n = hash(word);
  33.  
  34. // Iterates through linked list
  35. for (node *tmp = table[n]; tmp != NULL; tmp = tmp->next)
  36. {
  37. // Compares string case-insensitive
  38. if (strcasecmp(tmp->word, word) == 0)
  39. {
  40. // If word is in dictionary, return true and end loop
  41. return true;
  42. break;
  43. }
  44. }
  45. // Else return false
  46. return false;
  47. }
  48.  
  49. // Hashes word to a number
  50. unsigned int hash(const char *word)
  51. {
  52. // Initialise an int for length of the string and n
  53. int length = 0;
  54. int n = 0;
  55.  
  56. // Count number of characters in the word
  57. for (int i = 0; word[i] != '\0'; ++i)
  58. {
  59. // Adds one to length for each character
  60. length++;
  61. }
  62.  
  63. // For each character in the word
  64. for (int j = 0; j < length; j++)
  65. {
  66. // Adds character's ASCII value to the count n
  67. n += tolower(word[j]);
  68. }
  69. // Returns remainder of n divided by N such that return value never greater than N
  70. return (n % N);
  71. }
  72.  
  73. // Loads dictionary into memory, returning true if successful, else false
  74. bool load(const char *dictionary)
  75. {
  76. // Open file of dictionary
  77. FILE *dict = fopen(dictionary, "r");
  78.  
  79. // If dictionary is not present
  80. if (dictionary == NULL)
  81. {
  82. printf("Unable to open dictionary");
  83. return false;
  84. }
  85.  
  86. // Char array to store each word going to be added
  87. char buffer[LENGTH + 1];
  88.  
  89. // Read strings from file
  90. while (fscanf(dict, "%s", buffer) != EOF)
  91. {
  92. // Allocate memory for each word and node
  93. node *n = malloc(sizeof(node));
  94. if (n == NULL)
  95. {
  96. return false;
  97. }
  98.  
  99. // Copy word into node
  100. strcpy(n->word, buffer);
  101.  
  102. // Hash the word to get a hash value for the word
  103. unsigned int u = hash(buffer);
  104.  
  105. // If head does not point to anything
  106. if (table[u] == NULL)
  107. {
  108. // Set n to be the first element
  109. table[u] = n;
  110. }
  111. else
  112. {
  113. // Set n to point to first element
  114. n->next = table[u];
  115.  
  116. // Set head to point to n, making it the first element of linked list and n->next becomes second element
  117. table[u] = n;
  118. }
  119. count++;
  120. }
  121. // Close file of dictionary
  122. fclose(dict);
  123. return true;
  124. }
  125.  
  126. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  127. unsigned int size(void)
  128. {
  129. // Returns the global variable count
  130. return count;
  131. }
  132.  
  133. // Unloads dictionary from memory, returning true if successful, else false
  134. bool unload(void)
  135. {
  136. // Iterate through each bucket in hash table
  137. for (int i = 0; i < N; i++)
  138. {
  139. // Create new node called cursor that is equal to table[i]
  140. node *cursor = table[i];
  141.  
  142. // While the cursor is pointing at something
  143. while (cursor != NULL)
  144. {
  145. // Set node tmp to what the cursor is pointing at
  146. node *tmp = cursor;
  147.  
  148. // Set cursor to the next element in linked list
  149. cursor = cursor->next;
  150.  
  151. // Free tmp
  152. free(tmp);
  153. }
  154. // If cursor pointing at nothing and i reaches last bucket, dictionary successfully unloaded and return true
  155. if ((cursor == NULL) && (i == N - 1))
  156. {
  157. return true;
  158. }
  159. }
  160. // Else return false
  161. return false;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement