I have a struct defined as:
struct ac {
int value;
char character;
char * word;
struct ac *next;
struct ac *previous;
struct ac *child;
struct ac *parent;
};
I specifically have a problem when a word contains the same character twice or more at the end of the word.
// 'w' is the word input
int depth = 0;
ac *a; // is struct that is size of ac and contains all of the struct values set either to NULL or 0.
while(w[depth] != '\0'){
if (a -> character == w[depth]) {
if ((a -> value == 0) && (w[depth +1] == '\0')) {
a -> value = 1;
a -> word = malloc(strlen(w)+1);
strcpy(a -> word, w);
}
printf("follow existing path %c\n", w[depth]);
a = a -> child;
depth ++;
}
// word() is a function that reserves memory for the new word and initializes new_word problaply not relevent for this question.
else if (a -> child == NULL) {
new_word = word(w,depth);
new_word -> parent = a;
printf("create child %c\n", w[depth]);
a -> child = new_word;
a = new_word;
depth ++;
}
}
for example, when the word 'well' is the input the following output wil be printed:
- create child w
- create child e
- create child l
- follow existing path l
But this last 'follow existing path l' should have been 'create child l'
and i can't seem to think of a condition that wil discriminate against that last 'l'. Can someone maybe help me with this? it would be much appreciated.