Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- //opens the dictionary and checks that it is not null
- FILE *dict = fopen(dictionary, "r");
- if (dict == NULL)
- {
- return false;
- }
- //defines word for our program
- char word[LENGTH + 1];
- //loops through entirety of dictionary one word at a time until reaching EOF (end of file)
- while (fscanf(dict, "%s", word) != EOF)
- {
- //update word counter for the size funtion
- wordcount++;
- //creates a pointer to a new node and checks to make sure it is not null
- node *n = malloc(sizeof(node));
- if (n == NULL)
- {
- unload();
- return false;
- }
- //writes each word from the dictionary to a new node ??? possibly wrong place for this code or wrong entirely
- strcpy(n->word, word);
- n-> next = NULL;
- //initializes an index for where to store in our table
- int i;
- //hashes the word and assigns a value that we are storing as our index
- i = hash(word);
- //create an anchor to start linked list
- node *head = malloc(sizeof(node));
- if (head == NULL)
- {
- return false;
- }
- //insert node into hash table
- if (table[i] == NULL)
- {
- table[i] = n;
- head = n;
- }
- else
- {
- node *temp = malloc(sizeof(node));
- if (temp == NULL)
- {
- return false;
- }
- temp = n;
- temp->next = table[i];
- head->next = temp;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment