I am having problems with implementing unload. load works perfectly and I can use the check function. I have tried many ways of changing my code but it only creates an infinite loop and I have no idea why. Does anyone know where there are the problems?
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// prototypes
bool check(const char* word);
bool load(const char* dictionary);
unsigned int size(void);
// define node type for load
typedef struct node{
bool is_word;
struct node* children[27];
} node;
// additional prototypes
int ret_index(int letter);
// set starting pointer root as global variable
node* root;
/*declare node pointers to point to the leaves of the trie
(each indicating a level in the trie for the unload function) */
node* leaf[46];
// set integer pointer for level in unload function
int number = 0;
int* level = &number;
/* set integer pointer for counting number of words
(counted in load but returned in size function) */
unsigned int* dic_size;
bool load(const char* dictionary)
{
// open dictionary file
FILE* fp = fopen(dictionary, "r");
if (fp == NULL)
{
printf("Cannot open dictionary file!\n");
return false;
}
// create head root for trie
root = malloc(sizeof(node));
// initialize count for size of words in dictionary (for size function)
dic_size = malloc(sizeof(unsigned int));
*dic_size = 0;
// create node pointer traverse to move around trie
node* traverse = root;
// get char from dictionary
int c;
// iterate through dictionary
for (c = fgetc(fp);c != EOF; c = fgetc(fp))
{
// load the letter to trie
if (c != '\n')
{
// index used inside children[#]: ' = 0 and a ~ z = 1 ~ 27
int index = ret_index(c);
// if there is no node, create a new node and connect it
if (traverse -> children[index] == NULL)
{
node* new_node = malloc(sizeof(node));
for (int i = 0; i < 27; i++)
{
new_node -> children[i] = NULL;
}
traverse -> children[index] = new_node;
}
// move to new node to traverse
traverse = traverse -> children[index];
}
// if new line (end of word), signal true and point traverse to root to start new word
else
{
// put signal is_word to indicate end of word
traverse -> is_word = true;
// increment count of size (to use in size function)
*dic_size += 1;
// prepare for new word
traverse = root;
}
}
// eof: put end of the word
traverse -> is_word = true;
// increment count of size
*dic_size += 1;
// close dictionary file
fclose(fp);
// signal function is completed; else return false
if (c == EOF)
{
return true;
}
return false;
}
bool unload(void)
{
/* set a temporal node pointer to traverse
and to wire leaf[*level] with the loaded nodes */
node* traverse = root;
// declare bool variable to check whether child of current node is unloaded
bool checkUnload;
// iterate through all the children node pointer
for (int i = 0, n = 27; i < n; i++)
{
// wire the leaf[*level] pointers to the loaded nodes
leaf[*level] = traverse;
// if there is no path
if (leaf[*level] -> children[i] == NULL)
{
// base case: if last node is null
if (i == 26)
{
// free the node itself
free(leaf[*level]);
// update value of level and traverse pointer
*level = *level - 1;
traverse = leaf[*level];
// return value
if (leaf[*level] == NULL)
{
return true;
}
return false;
}
}
// else, when there is a path, go down (use recursion)
else
{
// update level and traverse pointer
*level = *level + 1;
traverse = traverse -> children[i];
// recursion
checkUnload = unload();
}
}
// check if successfull and return value
if (root == NULL)
{
return true;
}
return false;
}
// change value of letter into index value for use in trie
int ret_index(int letter)
{
// check whether valid input was given
if (!isalpha(letter) && letter != '\'')
{
printf("invalid letter given to re_index!\n");
}
// index used inside children[#]: ' = 0, a ~ z = 1 ~ 27
int index;
if (isalpha(letter))
{
index = letter - 96;
}
else
{
index = letter - 39;
}
return index;
}