I'm having a bit of trouble with making a "tree" representation of a linked list. That is, a root pointer points to two or more other pointers, and they point to 2 or more OTHER pointers, and it branches out from there. I'd prefer to do this over the "train" representation of all the pointers pointing to each other in a line, and having to traverse through the whole list to get to certain 'nodes'.
I just don't know how to make the 'root' pointer point to MORE than one object.
I'm not the best with linked lists...I don't really know how to create base pointers that can point to several other pointers. How's this done?Code:#include <iostream>
using namespace std;
struct Animal{
int Age;
Animal *next; //Pointer to the next Animal object
/*Constructor that initially sets ages to 15 and new pointers to 0*/
Animal():Age(15),next(0){cout<<"Just an animal created.\n";}
virtual void Act(){}; //Not very important
};
struct Cat: public Animal{
Cat(){cout<<"Kitty created.\n";}
void Act(){cout<<"Meow!";};
};
struct Dog: public Animal{
Dog(){cout<<"Doggy created.\n";}
void Act(){cout<<"Woof!";};
};
int main(void){
Animal *root = new Animal; //Create pointer to new Animal
root->next = new Dog; /*Pointer in new Animal goes to a new Dog
(But how can I have this point to BOTH a Dog and a CAT?*/
cin.get();
return 0;
}