0

Here is what I have come up with. I am looking for maybe some guidance as to what is going on so I might fix it. Here the section in question. It never evaluates to true.

if(current_Node-> chilluns[letter - 'a'] == NULL)
{
current_Node = current_Node->chilluns[letter - 'a'];
 i++;
 }
        else
        {
            return false;
        }

} // close loop



if(inchar == '\0' && current_Node-> is_Word) // if word exists
{
    return true;  
} 
else
{
    return false;
}    

}

So my questions are: 1. It is my understanding through the use of global variables I should be able to use current_Node-> is word as a comparison. Is this wrong? 2. If that is the case, have I done so incorrectly? 3. If it isn't could someone point me in a direction (links to read, videos, etc) that might be of use.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.h"
#include <ctype.h>
#include <stdbool.h>
#define NUM_NODES 27
int wordCount = 0;
typedef struct trie
{
bool is_Word;
struct trie* chilluns[27];
} 
trie;
trie* current_Node;
trie* root;

bool load(const char* dictionary)
{
//initializing setup stuffs
int c = 0;
trie* root = calloc(1, sizeof(trie));  
trie* current_Node = root; 
FILE* file = fopen(dictionary, "r");
if(!file)
{
    return 1;
}

//loop through the trie starting at the begining of dictionary

//traverse the dictionary
while((c = fgetc(file)) != EOF)
{
// current_Node = root;
if(c == '\n')
{
    if(&current_Node != &root && c == 10)
    {
        if (!current_Node->is_Word == true)
        {
        //in case of duplicates.
        current_Node->is_Word = true; //once line break reached end the word
        wordCount++;
        }

        current_Node = root;

    }
     else
     {
        c = fgetc(file);
     }
}
for(c = fgetc(file);c != '\n' && c != EOF; c = fgetc(file))
{
    if(!current_Node->chilluns[c - 'a'])
    { 
        current_Node->chilluns[c - 'a'] = calloc(1, sizeof(trie));
        current_Node = current_Node-> chilluns[c - 'a'];
    }


}

    current_Node->is_Word = true;
    wordCount++;  

}
    fclose(file);
    return true;
}
3
  • Are you sure your dictionary is even loading? Your struct typedef won't work. You haven't implemented a linked list (you need a *next node to in your struct to link to the next letter in the list). What your struct needs are: a variable to store the letter value, and a next node to link to the next letter (called child) in the list. – ronga Apr 18 '15 at 8:54
  • @ronga I added the load function to my dictionary. It comes up with the appropriate number of words in the large dictionary. When the program runs it says WORDS IN DICTIONARY: 143091 TIME IN LOAD: .19 – wtfbbq81 Apr 18 '15 at 19:17
  • Sorry, my bad, I must've misread your typedef. I used a tree of whole words myself, but here's something on tries I found helpful: cs.bu.edu/teaching/c/tree/trie. Rather than have a bool isword variable, you might try what the article proposes and determine if a word is in the dictionary with a null character at the end of a whole word. Another thing that might be throwing you off is the else c=fgetc(file) in your load function. You already had a while c=fgetc(file)!=EOF, your latter c=fgetc(file) will overwrite the 1st c value, and you will be missing letters. – ronga Apr 20 '15 at 7:26

You must log in to answer this question.

Browse other questions tagged .