shahzadb13

Untitled

Mar 1st, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /**
  2. * dictionary.c
  3. *
  4. * Computer Science 50
  5. * Problem Set 5
  6. *
  7. * Implements a dictionary's functionality.
  8. */
  9.  
  10. #include <stdbool.h>
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <strings.h>
  15. #include <string.h>
  16. #include "dictionary.h"
  17. #define BUCKETS 27
  18. //integer for counting dict words added
  19. int counter=0;
  20. //struct for hash table
  21. typedef struct tnode
  22. {
  23. char cword[LENGTH+1];
  24. struct tnode* next;
  25. }node;
  26. //node pointer array as buckets
  27. node* dic[BUCKETS];
  28.  
  29. //hash function for hash table
  30. int hash_f(char* word);
  31.  
  32. /**
  33. * Returns true if word is in dictionary else false.
  34. */
  35. bool check(const char* word)
  36. {
  37.  
  38. const int currlen=strlen(word);
  39.  
  40. char cbuffer[LENGTH+1];
  41.  
  42. for(int i=0;i<currlen;i++)
  43. {
  44. cbuffer[i]=tolower(word[i]);
  45.  
  46. }
  47.  
  48. cbuffer[currlen]='\0';
  49. int cbucket=hash_f(cbuffer);
  50. node *cursor = dic[cbucket];
  51. //iterate over the linked list at cbucket
  52. while(cursor!=NULL)
  53. {
  54. if((strcmp(cbuffer,cursor->cword)==0))
  55. {
  56. return true;
  57. }
  58. else
  59. cursor=cursor->next;
  60. }
  61.  
  62. return false;
  63. }
  64.  
  65. /**
  66. * Loads dictionary into memory. Returns true if successful else false.
  67. */
  68. bool load(const char* dictionary)
  69. {
  70. FILE *ptr = fopen(dictionary,"r");
  71. char buffer[LENGTH+1];
  72. int bucket;
  73. //check for availability of dictionary file
  74. if(ptr==NULL)
  75. {
  76. printf("could not open file \n");
  77. return false;
  78. }
  79. else
  80. {
  81. //iterate over dict
  82. while(fgets(buffer,sizeof(buffer),ptr))
  83. {
  84.  
  85. node* temp=malloc(sizeof(node));
  86. if(temp==NULL)
  87. {
  88. fclose(ptr);
  89. printf("SORRY NO MEMORY FOR TEMP!\n");
  90. return false;
  91. }
  92. strcpy(temp->cword,buffer);
  93. bucket=hash_f(temp->cword);
  94.  
  95. if(dic[bucket]==NULL)
  96. {
  97.  
  98. temp->next=dic[bucket];
  99. dic[bucket]=temp;
  100. }
  101. else
  102. {
  103.  
  104. temp->next=dic[bucket];
  105. dic[bucket]=temp;
  106.  
  107.  
  108. }
  109.  
  110. counter++;
  111.  
  112. }
  113. }
  114. fclose(ptr);
  115. return true;
  116. }
  117.  
  118. /**
  119. * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  120. */
  121. unsigned int size(void)
  122. {
  123. return counter;
  124. }
  125.  
  126. /**
  127. * Unloads dictionary from memory. Returns true if successful else false.
  128. */
  129. bool unload(void)
  130. {
  131.  
  132. return true;
  133. }
  134. //hash function implementation
  135. int hash_f(char* word)
  136. {
  137. int hash=0;
  138. if(isalpha(word[0]))
  139. {
  140. hash=word[0]-'a';
  141. }
  142. else if(word[0]=='\'')
  143. {
  144. hash=26;
  145. }
  146. hash=hash%BUCKETS;
  147. return hash;
  148. }
Add Comment
Please, Sign In to add comment