Guest User

Untitled

a guest
May 5th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. // Loads dictionary into memory, returning true if successful else false
  2. bool load(const char *dictionary)
  3. {
  4.  
  5. //opens the dictionary and checks that it is not null
  6. FILE *dict = fopen(dictionary, "r");
  7. if (dict == NULL)
  8. {
  9. return false;
  10. }
  11. //defines word for our program
  12. char word[LENGTH + 1];
  13. //loops through entirety of dictionary one word at a time until reaching EOF (end of file)
  14. while (fscanf(dict, "%s", word) != EOF)
  15. {
  16. //update word counter for the size funtion
  17. wordcount++;
  18. //creates a pointer to a new node and checks to make sure it is not null
  19. node *n = malloc(sizeof(node));
  20. if (n == NULL)
  21. {
  22. unload();
  23. return false;
  24. }
  25. //writes each word from the dictionary to a new node ??? possibly wrong place for this code or wrong entirely
  26. strcpy(n->word, word);
  27. n-> next = NULL;
  28. //initializes an index for where to store in our table
  29. int i;
  30. //hashes the word and assigns a value that we are storing as our index
  31. i = hash(word);
  32. //create an anchor to start linked list
  33. node *head = malloc(sizeof(node));
  34. if (head == NULL)
  35. {
  36. return false;
  37. }
  38. //insert node into hash table
  39. if (table[i] == NULL)
  40. {
  41. table[i] = n;
  42. head = n;
  43. }
  44. else
  45. {
  46. node *temp = malloc(sizeof(node));
  47. if (temp == NULL)
  48. {
  49. return false;
  50. }
  51. temp = n;
  52. temp->next = table[i];
  53. head->next = temp;
  54. }
  55.  
  56. }
  57. return true;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment