Advertisement
BurbankShirtface

Untitled

Jun 27th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdbool.h>
  4. #include <strings.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.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. // Number of buckets in hash table
  20. const unsigned int N = 1000;
  21.  
  22. // Hash table
  23. node *table[N];
  24.  
  25. // Returns true if word is in dictionary else false
  26. bool check(const char *word)
  27. {
  28. node* temp = NULL;
  29. int h = hash(word);
  30. temp = table[h];
  31.  
  32. do
  33. {
  34. int result = strcasecmp(word, temp->word);
  35.  
  36. if (result == 0)
  37. {
  38. return true;
  39. break;
  40. }
  41. else
  42. {
  43. temp = temp->next;
  44. }
  45. }
  46. while (temp->next != NULL);
  47.  
  48. return false;
  49. }
  50.  
  51. /* Adapted by Neel Mehta from
  52. http://stackoverflow.com/questions/2571683/djb2-hash-function.
  53. */
  54.  
  55. // Hashes word to a number
  56. unsigned int hash(const char *word)
  57. {
  58. unsigned long hash = 5381;
  59.  
  60. for (const char* ptr = word; *ptr != '\0'; ptr++)
  61. {
  62. hash = ((hash << 5) + hash) + tolower(*ptr);
  63. }
  64.  
  65. return hash % N;
  66.  
  67. }
  68.  
  69. // Loads dictionary into memory, returning true if successful else false
  70. int count;
  71. bool load(const char *dictionary)
  72. {
  73. FILE *dict = fopen(dictionary, "r");
  74. if (dict == NULL)
  75. {
  76. printf("Could not open dictionary.\n");
  77. return false;
  78. }
  79.  
  80. char temp[46];
  81. count = 0;
  82.  
  83.  
  84. while (fscanf(dict, "%s", temp)!= EOF)
  85. {
  86. node* n = malloc(sizeof(node));
  87. if (n == NULL)
  88. {
  89. free(n);
  90. return false;
  91. }
  92. else
  93. {
  94. int h = hash(temp);
  95. strcpy(n->word, temp);
  96. n->next = table[h];
  97. table[h] = n;
  98. count++;
  99. }
  100. }
  101. fclose(dict);
  102. return true;
  103. }
  104.  
  105. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  106. unsigned int size(void)
  107. {
  108. if (!count || count == 0)
  109. {
  110. return 0;
  111. }
  112. else
  113. {
  114. return count;
  115. }
  116. }
  117.  
  118. // Unloads dictionary from memory, returning true if successful else false
  119. bool unload(void)
  120. {
  121. node* temp = NULL;
  122. for (int i = 0; i <= N; i++)
  123. {
  124. temp = table[i];
  125. while (temp->next != NULL)
  126. {
  127. table[i] = temp->next;
  128. free(temp);
  129. temp = table[i];
  130. }
  131. }
  132. return true;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement