Guest User

Untitled

a guest
Jun 8th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. bool load(const char* dictionary)
  2. {
  3. char buffer[LENGTH + 1]; //temporary buffer for word
  4. int hashindex = 0; // Use hash function to initate an index integer
  5.  
  6. // open the dictionnary file (reading)
  7. FILE* dictionaryfile = fopen(dictionary, "r");
  8.  
  9. // check if the dictionary opens
  10. if (dictionaryfile == NULL)
  11. {
  12. return false;
  13. }
  14.  
  15. while(fscanf(dictionaryfile,"%s", buffer) != EOF)
  16. {
  17. node* new_node = malloc(sizeof(node)); // Allocate memory for new_node
  18. new_node->word = malloc(sizeof(word) +1);
  19.  
  20. strcpy(new_node->word, buffer);
  21.  
  22. hashindex = hash(new_node->word);
  23.  
  24. counter++; // Count the words
  25.  
  26. // Assign next to NULL if nothing is left in hashtable
  27. if (hashtable[hashindex] == NULL)
  28. {
  29. hashtable[hashindex] = new_node;
  30. new_node->next = NULL;
  31. }
  32.  
  33. // When node exists insert, and point to next
  34. else
  35. {
  36. new_node->next = hashtable[hashindex]->next;
  37. hashtable[hashindex] = new_node;
  38. }
  39. }
  40.  
  41. // Close
  42. fclose(dictionaryfile);
  43. // Success
  44. return true;
  45. }
Add Comment
Please, Sign In to add comment