Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. /**
  2. * dictionary.c
  3. *
  4. * David J. Malan
  5. * malan@harvard.edu
  6. *
  7. * Implements a dictionary's functionality using a hash table.
  8. */
  9.  
  10. #include <ctype.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <strings.h>
  16.  
  17. #include "dictionary.h"
  18.  
  19.  
  20.  
  21. /**
  22. * Loads dictionary into memory. Returns true if successful else false.
  23. */
  24.  
  25. bool load(const char* dictionary)
  26. {
  27. // TODO
  28. //w = 0; //crawler!!!
  29. //Loading files:
  30. //node* new_node = malloc(sizeof(node));
  31. //node* head;
  32. //node* new_node;
  33.  
  34. FILE* file = fopen(dictionary, "r");
  35. char *cur = NULL;
  36. fscanf(file, "%s", cur);
  37.  
  38. int hashed;
  39.  
  40. while(cur !=NULL) {
  41. hashed = hash(cur);
  42.  
  43. node* head = table[hashed];
  44. node* new_node = malloc(sizeof(node));
  45. // new_node->next = head;
  46. // table[hashed] = new_node;
  47. //w++;
  48.  
  49. if (head == NULL) {
  50. head = new_node;
  51. strcpy(new_node-> word, cur);
  52. table[hashed] = new_node;
  53. //w++;
  54. }
  55. //struct node *current = table[hashed];
  56. else{
  57. new_node->next = head;
  58. strcpy(new_node-> word, cur);
  59. table[hashed] = new_node;
  60. //w++;
  61. }
  62.  
  63. // w++;
  64. fscanf(file, "%s", cur);
  65. }
  66.  
  67. return true;
  68. }
  69.  
  70. /**
  71. * Returns number of words in dictionary if loaded. Else 0 if not yet loaded.
  72. */
  73. unsigned int size(void)
  74. {
  75. int n = 0;
  76. // printf("here!");
  77. for(int i = 0; i < BUCKETS+1; i++) {
  78. //printf("for loop: %d\n", i);
  79.  
  80. if (table[i] == NULL) {
  81.  
  82. printf("head null, i= %d, n = %d\n", i, n);
  83.  
  84. }
  85.  
  86. else {
  87.  
  88. printf("head not null, i= %d\n", i);
  89. node* crawler = table[i];
  90. while(crawler->next != NULL){
  91. printf("here!");
  92. n++;
  93. crawler = crawler->next;
  94. // printf("here!");
  95. }
  96.  
  97.  
  98. //
  99. }
  100. }
  101. return n;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement