user2648875
user2648875

Reputation: 39

Defining trees with unknown number of children

I need to define a tree wherein the number of children every node has is unknown. It cannot be done using an array because the number of children can be over or under estimated. So I need to use a linked list where the list will be of children of the node.

How will this be done

class emp
{
    string name;
    emp* parent;
    employee* child;

};

struct employee
{
    emp* junior;
    employee* next;
}; 

In this code employee is not defined before employee child is called so it gives an error. Please suggest changes.

Upvotes: 1

Views: 857

Answers (2)

Mats Petersson
Mats Petersson

Reputation: 129454

I personally would say that you should use a vector<employee> or `list rather than building your own linked list.

Note also that using a linked list will add a fair amount of extra memory usage, because each node till have an overhead of, typically, 32-64 bytes.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

Forward declare employee before emp class definition.

struct employee;

class emp
{
  //....

Upvotes: 1

Related Questions