C++ 循环队列基本算法实现
#ifndef CircleQueue_h#define CircleQueue_hconst int QueueSize = 1000;templateclass CircleQueue{public: CircleQueue(){front = rear = 0;} void EnQueue(T x); T DeQueue(); T GetFront(); void SetNull(); int GetLength(); bool Empty(){ return front == rear ? true : false;}private: T data[QueueSize]; int front; int rear;};template void CircleQueue ::EnQueue(T x){ if((rear+1)%QueueSize == front) throw "overflow"; rear = (rear + 1)%QueueSize; //队尾指向下一个位置 data[rear] = x;}template T CircleQueue ::DeQueue(){ if(front == rear) throw "the Queue is null"; front = (front+1)%QueueSize; T x = data[front]; return x;}template T CircleQueue ::GetFront(){ if(front == rear) throw "the Queue is null"; return data[(front +1)%QueueSize];}template int CircleQueue ::GetLength(){ return (rear-front +QueueSize)%QueueSize;}template void CircleQueue ::SetNull(){ rear = 0; front = 0;}#endif /* CircleQueue_h */