CirQueue.h
#ifndef __CIRQUEUE_H__ #define __CIRQUEUE_H__ const int queueSize = 100; template <class T> class CirQueue { public: CirQueue(); ~CirQueue(); void EnQueue(T _data); T DeQueue(); T getQueue(); bool isEmpty(); bool isFilled(); private: int front, rear; T data[queueSize]; }; #endif
CirQueue.cpp
#include "CirQueue.h" #include <iostream> using namespace std; template <class T> CirQueue<T>::CirQueue() { rear = front = 0; } template <class T> CirQueue<T>::~CirQueue() {} template <class T> void CirQueue<T>::EnQueue(T _data) { if (!isFilled()) { rear = (rear + 1) % queueSize; data[rear] = _data; } } template <class T> T CirQueue<T>::DeQueue() { if (!isEmpty()) { front = (front + 1) % queueSize; T tempData = data[front]; data[front] = 0; return tempData; } else { cout << "Queue is empty"; return false; } } template <class T> T CirQueue<T>::getQueue() { int tempPos = (front + 1) % queueSize; return data[tempPos]; } template <class T> bool CirQueue<T>::isEmpty() { if (front == rear) return true; return false; } template <class T> bool CirQueue<T>::isFilled() { int tempRear = (rear + 1) % queueSize; if (front == tempRear) return true; return false; }
queue.cpp
#include <iostream> #include "CirQueue.h" #include "CirQueue.cpp" using namespace std; int main() { CirQueue<char> cirq; cirq.EnQueue('A'); cirq.EnQueue('B'); cirq.EnQueue('C'); cirq.EnQueue('D'); cout << cirq.getQueue() << endl; cout << cirq.DeQueue() << endl; cout << cirq.DeQueue() << endl; cout << cirq.DeQueue() << endl; cout << cirq.DeQueue() << endl; cout << cirq.DeQueue() << endl; return 0; }