Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool load(const char* dictionary)
- {
- char buffer[LENGTH + 1]; //temporary buffer for word
- int hashindex = 0; // Use hash function to initate an index integer
- // open the dictionnary file (reading)
- FILE* dictionaryfile = fopen(dictionary, "r");
- // check if the dictionary opens
- if (dictionaryfile == NULL)
- {
- return false;
- }
- while(fscanf(dictionaryfile,"%s", buffer) != EOF)
- {
- node* new_node = malloc(sizeof(node)); // Allocate memory for new_node
- new_node->word = malloc(sizeof(word) +1);
- strcpy(new_node->word, buffer);
- hashindex = hash(new_node->word);
- counter++; // Count the words
- // Assign next to NULL if nothing is left in hashtable
- if (hashtable[hashindex] == NULL)
- {
- hashtable[hashindex] = new_node;
- new_node->next = NULL;
- }
- // When node exists insert, and point to next
- else
- {
- new_node->next = hashtable[hashindex]->next;
- hashtable[hashindex] = new_node;
- }
- }
- // Close
- fclose(dictionaryfile);
- // Success
- return true;
- }
Add Comment
Please, Sign In to add comment