1

I've been stuck on load for a while now (like 1 month...) and I've advanced quite a lot but I'm so close to completing it but, unfortunately, I've reached a bit of a dead end when it comes to checking if the node is NULL and creating a new one.

Currently, it is unable to recognize next_node->children[asci] is NULL and doesn't create a new node. Only when it reaches the last letter of the node does the node think the node[asci] already exists and doesn't create a new one.

The code is here below:

bool load(const char* dictionary)
{
    FILE* dictionary_file = fopen(dictionary, "r");
    if (dictionary_file == NULL)
    {
        printf("Dictionary not opened\n");
        return false;
    }
    printf("Dictionary successfully opened\n");

    typedef struct node
    {
        bool is_word;
        struct node* children[27];
    }
    node;
    printf("Created typedef\n");

    while (!feof(dictionary_file))
    {
        node* root = calloc(1, sizeof(node));
        printf("Created node* root\n");

        // make string to hold each word in dictionary
        char word[LENGTH + 2];
        int word_i[LENGTH + 2];
        // scans and stores each string in word
        fgets(word, LENGTH + 2, dictionary_file);

        //measures length of word
        int wordlength = strlen(word);
        printf("wordlength is %i\n", wordlength);

        for (int i = 0; i < wordlength; i++)
        {
            word_i[i] = word[i];
            node* next_node = root;
            int asci = 0;

            //check if characters in word are letters
            if (isalpha(word[i]))
            {
                // make asci 0 to 27 depending on letters
                if (islower(word[i]))
                {
                    asci = word_i[i] - 97;
                }
                else
                {
                    asci = word_i[i] - 65;
                }
                printf("asci = %i\n", asci);

                //if the letter isn't found in the root node make a new node
                if (next_node->children[asci] == NULL)
                {
                    //use a pointer to the place in the node where the letter should be                    //then allocate memory for another node
                    //make the pointer equal to the next new node
                    next_node->children[asci] = calloc(1, sizeof(node));
                    next_node = next_node->children[asci];

                    //Check if at end of the word
                    if (word[i + 1] == '\'')
                    {
                        next_node->is_word = true;
                    }
                }
                else
                {
                    printf("Node already exists no need to create a new one");
                    next_node = next_node->children[asci];
                }
            }
            else
            {
                printf("Wasn't in alphabet \n");
            }
        }
    }
    return true;
}

Any help would be thoroughly appreciated!

4
  • Are you sure you want to create a new root node for every word in dictionary? while (!feof(dictionary_file)) { node* root = calloc(1, sizeof(node)); – ChrisG Aug 2 '16 at 6:13
  • 1
    Oh yeah! thanks for that. So if I place that outside the while clause, would there only be an original root node and then using that, I add on a new node at the end of the while clause if needed... – Junho Choi Aug 2 '16 at 6:17
  • That is correct! :) – ChrisG Aug 2 '16 at 7:50
  • Glad you sorted it out. Can you add an answer describing your solution and accept it to remove this from the unanswered question pool? thanks. – Cliff B Aug 11 '16 at 20:43

You must log in to answer this question.

Browse other questions tagged .