Code:
template<class ItemType>
GLista<ItemType>::GList() //Constructor
{
length = 0;
sorted = false;
}
Constructors must have the same name as the class. So, either the name of the class must be GList or the constructor must be GLista.
Well, it looks like you started off with an array based container implementation and now wish to move to a list based container correct? Well, for starters, lists and arrays are two very different things. You can't just slap a "list" label on an array implementation and have it work like a list should work. You are going to need to start from scratch. It would also be helpful to have a complete class declaration to look at and not just the function themselves, one for a basic double-linked list might look something like:
Code:
template <class T> struct node
{
T data;
node *next;
node *previous;
node(const T& item)
{
next = previous = NULL;
data = item;
}
node()
{
next = previous = NULL;
}
};
template <class T>
class GList
{
node<T> *head;
node<T> *tail;
public:
GList()
{
head = tail = NULL;
}
~GList()
{
node<T> *current = head;
node<T> *temp;
while( current != NULL )
{
temp = current;
current = current->next;
delete temp;
}
}
void push_back(const T& data)
{
node<T> *pNew = new node<T>(data);
if( pNew )
{
if( head == NULL )
{
// This is the first node added to list, set head/tail pointers
head = tail = pNew;
}
else
{
// Previous nodes have been added, add this node to end of list
tail->next = pNew;
pNew->previous = tail;
tail = pNew;
}
}
else
{
// There was an error allocating memory, display error message
cerr << "GList::push_back - Memory allocation error!" << endl;
}
}
};
You'll need to convert all your necessary functions from array based implementations into a linked list implementation. I was nice and demonstrated a "push_back" function that adds a new node to the end of the list, you'll need to do the rest. Since you are using templates, all that probably needs to go in a header file of some kind. You'll need to decide what to keep from your old array implementation and what to throw out. Obviously, a list can keep growing (until no more memory is available) so a function like IsFull really doesn't make sense with a list unless you wish to place an arbitrary restriction on its size. When it comes time to use your GList class, you can start out using it like this:
Code:
GList<int> intList;
intList.push_back(10); // Add element to linked list
intList.push_back(20); // Add element to linked list
intList.push_back(30); // Add element to linked list
intList.push_back(40); // Add element to linked list