My assignment is to create a general tree, and I decided to do this using three classes: node (contains character data, a pointer to a list ADT which contains all its children, and a pointer to another node for its own sibling nodes), list (contains all the children of a node), and the tree itself. The list is a singly linked list of nodes. The node class is supposed to be used in both the list and tree ADTs. Whenever I try to test my code in main() and see whether the classes are being created properly, I keep getting the error message "error: unknown type name 'list'". Can anyone look at my code and help me figure out how to correctly set up my classes?
//Genghis Khan
#include <iostream>
using namespace std;
class node //will be used for both list and trie ADTs
{
char character;
list* child; //all the children of the node will be stored in the list
node* sibling; //within a list, provides access to all children, functions like a "next"
friend class list;
friend class trie;
};
class list
{
private:
node* head;
node* tail;
public:
list()
{
head = new node;
tail = new node;
head->character = '*'; //head is a sentinel
tail->character = '*';
tail->sibling = head;
}
void insert(char a) //insertion into the list at the tail
{
node* v= new node;
tail->sibling = v;
v->character = a;
v->child = NULL;
tail = v;
}
int size() //so that I can use an empty() boolean
{
int size = 0;
node* temp = head;
if(temp->character != '*')
{
size += 1;
temp = temp->sibling;
}
else
{
temp = temp->sibling;
}
return size;
}
bool empty() //empty condition
{
return(size() == 0);
}
void print()
{
node* temp = head;
while(temp->character != '*' and temp != tail)
{
cout << temp->character;
temp = temp->sibling;
}
}
friend class trie;
friend class node;
};
class trie
{
private:
node* root;
public:
trie()
{
root = new node;
root->character = '*'; //sentinel
root->child = NULL; //to be changed when we add children
root->sibling = NULL; //will never change
}
bool haschildren(node* v)
{
return(v->child != NULL);
}
};
int main()
{
list a;
a.insert('G');
a.insert('e');
a.insert('n');
a.insert('g');
a.insert('h');
a.insert('i');
a.insert('s');
a.print();
}
As a note, I tried making "node" a struct instead of a class, but that did not help.