0

Well. Pset5 is a pain so far, but I'm a masochist so no problem there.

I think my logic is okay, but I have a problem with naming my nodes. I can't seem to come up with a way to name my nodes differently. This may seem blurry, but let me paste in part of my code for better understanding:

// iterates through the dictionary
for (int s = fgetc(dic); s != EOF; s = fgetc(dic))
{

    if (s != '\n' && reset == 0)
    {
        s = s - 'a';

        if (root -> path[s] == NULL)
        {
            node* node1 = malloc(sizeof(node));
            root -> path[s] = node1;
        }

        reset++;
    }
    else if (s != '\n' && reset > 0)
    {
        s = s - 'a';

        if (node1 -> path[s] == NULL)
        {
            node* node2 = malloc(sizeof(node));
            node1 -> path[s] = node2;
        }

        reset++;
    }
    else
    {
        // currentnode -> is_word = true;
        reset = 0;
    }

}

Logic of this according to me

So what I'm trying to do there is the following:

  1. I'm trying to get every word from the dictionary character by character, thus iterating through the whole file.

  2. If the character is the first character of a word, then I check if in my root node that pointer 'space' in the array is NULL. If it is, I malloc a new node, and put a pointer to it from the root node.

  3. If the character isn't the first char, then I check the same thing, except this time not with the root node, but rather the next node (node1). and here I think is where the problem lies

  4. If the character being read by fgetc is newline, then I reset my counter, so I know a new word is coming, and indicate, that a word has ended (I know I need to refine this).

In summary: I have no idea how I could increase the nodecount (so afetr node1 comes a node 2, a node3 etc..)

Any ideas or hints? I think I smell that my logic is faulty in some way, but can't seem to see how.

Edit: Edited a bit cause I modified some var names.

1 Answer 1

1

Okay. It seems my knowledge on linked-lists was far from flawless. I have reviewed some of the videos and read up some more on the topic.

I'll begin with trying to put together a linked-list then move on to a trie (cause they seem very similar in spirit).

Sorry for the question, I kind of realize how foolish it was.

Well, time to atone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .