0

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.

5
  • 2
    You might want to describe exactly what you're trying to achieve Commented Mar 17, 2016 at 20:19
  • Are you sure it works if the repeated letters are in the middle of a word? Commented Mar 17, 2016 at 20:34
  • -Martin Broadhurst. You are correct it doesn't work either if repeating letters are in the middle of a word. I didn't try that. Commented Mar 17, 2016 at 20:40
  • Your struct ac does not contain a member named "letter", and you have a->letter, so is "a" some other kind of struct? Commented Mar 17, 2016 at 20:42
  • -FredK. Sorry i translated most of my code to english but a -> letter is supposed to be a -> character. Commented Mar 17, 2016 at 20:46

1 Answer 1

1

The problem is that you are not looking ahead properly. You should be checking if a->child is NULL first, adding a new node if it is and then moving to that child.

If a->child is not NULL, then you should compare a->child->character to the current character, and move to a->child if it matches.

I think it should look like this:

int depth = 0;
ac *a;
while (w[depth] != '\0') {
    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 ++;
    }
    else if (a->child->character == w[depth]) {
        if ((a->child->value == 0) && (w[depth +1] == '\0')) {
            a->child->value = 1;
            a->child->word = malloc(strlen(w)+1);
            strcpy(a->child->word, w);
        }
        printf("follow existing path %c\n", w[depth]);
        a = a->child;
        depth ++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.