I am still stuck on pset5 speller.c. I am trying to implement unload via tries. I have tried several different codes which were not giving the correct output, but the last one I did could not even compile. Please I need suggestions on how to implement the unload function effectively. Please take a look at my code and let me know what am doing wrong. Please help. Thank you.
/**
* dictionary.c
*
* Computer Science 50
* Problem Set 5
*
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
#define ALPHABETS 27
typedef struct node
{
bool is_word;
struct node* children[ALPHABETS];
}node;
node* root = NULL;
int counter =0;
node* cursor = root;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
// TODO
node* current = root;
int w_index;
int w_len = strlen(word);
for(int i = 0; i < w_len;i++)
{
if(isupper(word[i]))
w_index = (tolower(word[i])) - 'a';
else
w_index = word[i] - 'a';
if(current->children[w_index] == NULL)
{
current->is_word = false;
return false;
}
else
current = current->children[w_index];
}
if(current->is_word == true)
return true;
else
return false;
return true;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// TODO
char d_word[LENGTH +1];
root = (node*)malloc(sizeof(node));
if(root)
{
int i;
root->is_word = false;
for(i = 0; i < ALPHABETS; i++)
root->children[i] = NULL;
}
FILE* file = fopen(dictionary, "r");
if (file == NULL)
{
printf("Could not open %s.\n", dictionary);
unload();
return 1;
}
int index;
while(fscanf(file, "%s", d_word)!= EOF)
{
node*trav = root;
int length = strlen(d_word);
for(int j = 0; j < length; j++)
{
index = d_word[j] - 'a';
if(trav->children[index] == NULL)
trav->children[index] = malloc(sizeof(node));
trav = trav->children[index];
}
trav->is_word = true;
counter++;
}
fclose(file);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
// TODO
return (counter);
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
// TODO
node* temp;
for(int i = 0; i < ALPHABETS; i++)
{
if (cursor->children[i] != NULL)
{
temp = cursor;
cursor = cursor->children[i];
unload();
}
else
free(temp);
}
return true;
}