Playing with templates
Dumb strokes of C++ templates while bored generated this templated queue.
#define QueueSize 5 template <typename T> 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 <typename T> void Queue<T>::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 <typename T> T Queue<T>::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 <typename T> void Queue<T>::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; }