代码改变世界

【动手敲代码】:循环队列(C++)

2012-05-06 18:58  ATP_  阅读(458)  评论(0编辑  收藏  举报

#include<iostream>
 template <class T>
 class Queue
 {
 private:
     T rear;
     T front;
     int maxSize;
     T* head;
 public:
     Queue(int size):rear(0),front(0),maxSize(size)
     {
         head = new T(size);
         if(!head)
             std::cout << "memoryAllocationError!" << std::endl;
     }
     ~Queue()
     {
         maxSize = 0;
         delete[] head;
     }
     bool IsEmpty()
     {
         return front == rear;
     }
     bool IsFull()
     {
         return rear - front == maxSize;
     }
     void inQueue(T item)
     {
         if(IsFull())
             std::cout << "Queue is full!" << std::endl;
         *(head + rear) = item;
         rear ++;
     }
     T outQueue()
     {
         if(IsEmpty())
             std::cout << "Queue is empty!" << std::endl;
         T item = *(head + front);
         front ++;
         return item;
     }
     void Destroy()
     {
         ~Queue();
     }
     
 };