I am using the trie structure for doing the pset5 speller. However, my program can't compile due to 'redefinition of node' in 'typedef struct node', and 'declaration shadows a variable in the global scope in 'void free_all(node* root)'. I am not sure of how to solve the problems.
// Implements a dictionary's functionality
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "dictionary.h"
// Definition of node
typedef struct node
{
bool is_word;
struct node *children[27];
}
node;
node *root;
node* currentnode;
int numberofwords;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int numberword;
currentnode = root;
for(int j=0; j<strlen(word); j++)
{
// Turn to number (case-insensitive)
if(isalpha(word[j]))
{
if(isupper(word[j]))
{
numberword = word[j] - 'A';
}
if(islower(word[j]))
{
numberword = word[j] - 'a';
}
}
if(word[j]=='\'')
{
numberword = 26;
}
//Go to corresponding element in children (NULL: mispelled; otherwise to next character)
if(currentnode -> children[numberword] != NULL)
{
currentnode = currentnode -> children[numberword];
}
if(currentnode -> children[numberword] == NULL)
{
return false;
}
//End of word: Check is_word true
}
if(currentnode -> is_word == true)
{
return true;
}
else
{
return false;
}
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
currentnode = root;
//open file
FILE* largedictionary = fopen(dictionary, "r");
//Load words
char dictionaryword [45];
int number;
while(fscanf(largedictionary, "%s", dictionaryword) != EOF)
{
for (int i = 0; i<strlen(dictionaryword); i++)
{
//Convert character to number
if (isalpha(dictionaryword[i]))
{
number = dictionaryword[0]-'a';
}
if(dictionaryword[i]=='\'')
{
number = 26;
}
//Make nodes
if(currentnode->children[number] == NULL)
{
node *new_node = malloc(sizeof(node));
if(new_node == NULL)
{
unload();
return false;
}
new_node = currentnode->children[number];
currentnode = new_node;
}
}
numberofwords++;
}
return false;
fclose(largedictionary);
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return numberofwords;
}
// Unloads dictionary from memory, returning true if successful else false
//Function to free all
void free_all(node* root)
{
for (int m=0; m<27; m++)
{
if(!root)
{
return;
}
if(root->children[m]!=NULL)
{
free_all(root -> children[m]);
}
free(root);
}
}
//Freeing
bool unload(void)
{
free_all(root);
return false;
}
}
?