Advertisement
dawnrhema

speller.c imp

Oct 19th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <cs50.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <strings.h>
  7.  
  8.  
  9. #include <ctype.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12.  
  13. #include "dictionary.h"
  14.  
  15. #define Q 26 // rep number of buckets or lists in a hash table
  16.  
  17. int r = 0;
  18.  
  19. // Represents a node in a hash table
  20. typedef struct node
  21. {
  22. char word[LENGTH + 1];
  23. struct node *next;
  24. }
  25. node;
  26.  
  27. // Represents a hash table // now double array
  28. node *hashtable[Q];
  29.  
  30. // Hashes word to a number between 0 and 25, inclusive, based on its first letter
  31. unsigned int hash(const char *word)
  32. {
  33. return tolower(word[0]) - 'a';
  34. }
  35.  
  36. // Loads dictionary into memory, returning true if successful else false
  37. bool load(const char *dictionary)
  38. {
  39. // Initialize hash table
  40. for (int i = 0; i < Q; i++)
  41. {
  42. // init
  43. // hashtable[i] = NULL;
  44. // malloc a node for every word scan
  45. hashtable[i] = malloc(sizeof(node));
  46. // check if malloc is successful
  47. if (hashtable[i] == NULL)
  48. {
  49. unload();
  50. return false;
  51. }
  52. }
  53.  
  54. // Open dictionary
  55. FILE *file = fopen(dictionary, "r");
  56. if (file == NULL)
  57. {
  58. unload();
  59. return false;
  60. }
  61.  
  62. // Buffer for a word
  63. char word[LENGTH + 1];
  64.  
  65. // Insert words into hash table
  66. while (fscanf(file, "%s", word) && !feof(file)) // !EOF
  67. {
  68. // TODO
  69. r++; // counting no. of words
  70. // hash
  71. int y = hash(word);
  72. // malloc a node for every word scan
  73. /*hashtable[y] = malloc(sizeof(node)); // for dictionary file
  74. // check if malloc is successful
  75. if (hashtable[y] == NULL)
  76. {
  77. unload();
  78.  
  79. return false;
  80. }*/
  81. strcpy(hashtable[y]->word, word);
  82.  
  83. node *head = hashtable[y];
  84.  
  85. hashtable[y]->next = head;
  86. head = hashtable[y];
  87.  
  88. }
  89. // Close dictionary
  90. fclose(file);
  91. // Indicate success
  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 r;
  100. }
  101.  
  102. // Returns true if word is in dictionary else false
  103. bool check(const char *word)
  104. {
  105. // TODO
  106. int y = hash(word); // hash has to be deterministic
  107.  
  108. // compare
  109. if (strcasecmp(hashtable[y]->word, word) == 0)
  110. {
  111. return true;
  112. }
  113.  
  114. return false;
  115. }
  116.  
  117. // Unloads dictionary from memory, returning true if successful else false
  118. bool unload(void)
  119. {
  120. // TODO
  121.  
  122. for (int i = 0; i < Q; i++)
  123. {
  124. free(hashtable[i]); //
  125.  
  126. if (i == Q - 1) // if end
  127. {
  128. return true;
  129. }
  130. }
  131.  
  132. return false;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement