实现循环队列。假如循环队列的max_size为n,那么此种方法最多可以存储n-1个元素。用( (tail+1) % max_size) == head判断队满。用head == tail判断队空。 typedef int DATATYPE; class CycleQueue { private: int head; int tail; int max_size; DATATYPE* data; public: CycleQueue(int size); ~CycleQueue(); bool Enqueue(const DATATYPE& d); bool Dequeue(DATATYPE& d); }; CycleQueue::CycleQueue(int size) { this->max_size=size; data=new DATATYPE[size]; this->head=this->tail=0; } CycleQueue::~CycleQueue() { delete [] data; } bool CycleQueue::Enqueue(const DATATYPE& d) { if ( ( (tail+1) % max_size) == head ) return false; data[tail]=d; tail=(tail+1) % max_size; return true; } bool CycleQueue::Dequeue(DATATYPE& d) { if ( head == tail ) return false; d=data[head]; head=(head+1) % max_size; return true; }