It's possible that I'm not understanding your logic because I don't see all of the code, but something looks suspicious. It appears that you are reallocating the root node on every pass through the while loop. While the code may be working correctly on each pass, I believe it is trashing the previous pass and only creating the trie for the current word. If so, then only the last word will be in the dictionary. If this is true, you should either allocate the root node before the while loop or (less efficient) test to see if it exists before allocating it. I could be very wrong, but it should be investigated.
Also, it might be interesting to see what happens if foobar is the first word instead of the last word in your dictionary. Will only the last word remain, or will all 3 last words remain? I think it will still be only the last.
I haven't fully analyzed your code, but something else jumped out immediately. In function addchar(...)
, you call chr2idx(c);
which returns the index value idx
. Unfortunately, you don't do anything with that value. Whatever is in idx
in addchar()
is being used. I'm surprised it didn't give a compile error, unless idx is a global. Try fixing this and let us know if it works.
If this answers your question, please click on the check to accept and to remove this question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)