- //max length the array have
- #define MAX_ELEMENT 32
- template <class Type>
- class Queue{
- private:
- int count;
- int front,rear;
- Type queue_element[MAX_ELEMENT];
- public:
- Queue(){
- count=0;
- //head index of queue
- front=1;
- //tail index of queue
- rear=0;
- }
- inline bool empty(){
- return count<=0;
- }
- //0 append succeed;-1 append fail
- inline int append(const Type &in){
- if(count+1>MAX_ELEMENT)
- return -1;
- int i=rear+1>=MAX_ELEMENT?0:rear+1;
- queue_element[i]=in;
- rear=i;
- count++;
- return 0;
- }
- inline int serve(){
- if(count==0)
- return -1;
- front=front+1>=MAX_ELEMENT?0:front+1;
- count--;
- }
- inline int retrieve(Type &item){
- if(count<=0)
- return -1;
- item=queue_element[front];
- return 0;
- }
- inline int getsize(){
- return count;
- }
- };
- //该片段来自于http://www.codesnippet.cn/detail/0406201512759.html
来源: http://www.codesnippet.cn/detail/0406201512759.html