c++实现的数组队列
数组队列类源码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma once template<class T> class ArrayQueue { public: ArrayQueue(void) { front = 0; rear = 0; maxSize = 20; list = new T [maxSize]; } //向队尾插入元素 void in(T obj) { if(!IsFull()) { list[rear] = obj; rear=(rear+1)%maxSize; } } //从队头取出元素 T out() { if(!IsEmpty()) { int t= list[front]; front = (front+1)%maxSize; return t; } } bool IsEmpty() { if(front==rear) return true; else return false; } //循环队列,少用一个存储单元,队满条件:front==(rear+1)%maxSize bool IsFull() { if((rear+1)%maxSize==front) return true; else return false; } ~ArrayQueue(void); private: int front; int rear; int maxSize; T *list; };
测试代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//数组队列 ArrayQueue<int> * queue = new ArrayQueue<int>(); queue->in(3);queue->in(2);queue->in(7);queue->in(6); queue->in(8);queue->in(5);queue->in(4);queue->in(1); while(!queue->IsEmpty()) { std::cout<<queue->out()<<" "; }