template < typename ElemType, int iSize=100 > class Queue { public: Queue() { m_head = 0; m_rear = 0; m_rear = 0; } explicit Queue(const Queue<ElemType, iSize>& other); Queue<ElemType, iSize>& operator =(const Queue<ElemType, iSize>& other); ~Queue() { } bool empty(void) const { // test if queue is empty return (0 == m_rear); } bool full(void) const { // test if queue is full return (iSize == m_rear); } unsigned int size(void) const { // return length of queue return m_rear; } bool front(ElemType& value) { // return first element of mutable queue if (empty()) { printf("front: queue is empty !/n"); return false; } else { value = elems[m_head]; return true; } } const bool front(ElemType& value) const { // return first element of nonmutable queue if (empty()) { printf("front: queue is empty !/n"); return false; } else { value = elems[m_head]; return true; } } bool back(ElemType& value) { // return last element of mutable queue if (empty()) { printf("back: queue is empty !/n"); return false; } else { value = elems[m_rear]; return true; } } const bool back(ElemType& value) const { // return last element of nonmutable queue if (empty()) { printf("back: queue is empty !/n"); return false; } else { value = elems[m_rear]; return true; } } bool push(const ElemType& _Val) { // insert element at rear if (full()) { printf("push: queue is full !/n"); return false; } else { m_rear++; //队列元素个数加1 elems[m_rear] = _Val; //新元素插入队尾 m_rear = (m_rear + 1)%iSize; //循环意义下将尾指针加1 return true; } } bool pop(void) { // erase element at front if (empty()) { printf("pop: queue is empty !/n"); return false; } else { m_rear--; //队列元素个数减1 m_head = (m_head + 1)%iSize; //循环意义下将头指针加1 return true; } } friend bool operator==(const Queue<ElemType, iSize> & _Left,const Queue<ElemType, iSize> & _Right) { bool result = ((_Left.m_rear==_Right.m_rear)&&(_Left.m_head==_Right.m_head)&&(_Left.m_rear==_Right.m_rear)); if (!result) { return false; } else { for (int i=0; i<_Left.m_rear; i++) { if (_Left.elems[i] != _Right.elems[i] ) { return false; } } return true; } } friend bool operator!=(const Queue<ElemType, iSize> & _Left,const Queue<ElemType, iSize> & _Right) { return (!(_Left == _Right)); } private: ElemType elems[iSize]; int m_head; int m_rear; int m_count; }; template < typename ElemType, int iSize> Queue<ElemType, iSize>:: Queue(const Queue<ElemType, iSize>& other) { // construct by copying specified container m_head = other.m_head; m_rear = other.m_rear; m_rear = other.m_rear; for (int i=0; i<other.m_rear; i++) { elems[i] = other.elems[i]; } } template < typename ElemType, int iSize> Queue<ElemType, iSize>& Queue<ElemType, iSize>::operator =(const Queue<ElemType, iSize>& other) { if (this == &other) { return *this; } m_head = other.m_head; m_rear = other.m_rear; m_rear = other.m_rear; for (int i=0; i<other.m_rear; i++) { elems[i] = other.elems[i]; } return *this; }