Guest User

Untitled

a guest
May 14th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #include "dictionary.h"
  7.  
  8. /**
  9. * Returns true if word is in dictionary else false.
  10. */
  11.  
  12.  
  13. typedef struct node
  14. {
  15. bool is_word;
  16. // size of 26 alphabets + one apostrophe character = 27
  17. struct node* dependent[27];
  18. }
  19. node;
  20.  
  21. // declare root node
  22. node* root = NULL;
  23.  
  24. bool load(const char* dictionary)
  25. {
  26. // initialize root
  27. root = calloc(1, sizeof(node));
  28.  
  29. // identify each character of the words in dictionary
  30. for (int i = 0, l = strlen(dictionary); i < l; i++)
  31. {
  32. // initializing 1st letter of the word
  33. int k = dictionary[0] - 'a';
  34.  
  35. // placing alphabetical values starting from 0
  36. int j = dictionary[i] - 'a';
  37.  
  38. // if 1st position is NULL
  39. if (root -> dependent[k] == NULL)
  40. {
  41. root -> dependent[k] = dictionary[0];
  42.  
  43. // if single character word
  44. if (i == l - 1)
  45. {
  46. root -> is_word = true;
  47. }
  48. }
  49.  
  50. // if root position is not NULL and is a single character word
  51. else if (root -> dependent[k] != NULL && i == l - 1)
  52. {
  53. root -> is_word = true;
  54. }
  55.  
  56. else
  57. {
  58. // create and initialize new node
  59. node* new_node = calloc(1, sizeof(node));
  60.  
  61. // store character in new node pointed to by the root node
  62. root -> dependent[k] -> (new_node[i] -> dependent[j]) = dictionary[i];
  63.  
  64. if (i == l - 1)
  65. {
  66. root -> is_word = true;
  67. }
  68. }
  69. }
  70.  
  71. return false;
  72. }
Add Comment
Please, Sign In to add comment