Playing with templates

Dumb strokes of C++ templates while bored generated this templated queue.


#define QueueSize 5

template
class Queue
{

public:
Queue() : head(NULL), tail(-1) { for (int i=0; i < QueueSize; queue[i++]=NULL); }
void insert(T in);
bool isEmpty() { return ( queue[head] == NULL ) ? true : false; }
bool isFull() { return (head == tail && queue[head] != NULL) ? true : false; }
T remove();
void showContents();

private:
T queue[QueueSize];
int head, tail;

};

template
void Queue::showContents()
{

int loc=0, i=0;

if(isEmpty())
{
cout << "Empty" << endl;
return;
}

for (i=head; i < tail || i < QueueSize; i++)
cout << queue[i] << " ";

if(head > loc)
while(loc < tail && loc < head && queue[loc] != NULL)
cout << queue[loc++] << " ";

cout << endl;

}

template
T Queue::remove()
{
int retVal = 0;

if (!isEmpty())
{

retVal = queue[head];
queue[head] = NULL;

if (head == QueueSize - 1)
head = 0;
else
head++;

}
else
cout << "Stack Underflow" << endl;

return retVal;

}

template
void Queue::insert(T in)
{
int prevTail = tail;

if (tail == QueueSize - 1)
tail = 0;
else
tail++;

if (isFull())
{
cout << "Stack Overflow" << endl;
tail = prevTail;
}
else
queue[tail] = in;

return;
}

Tags: