循环队列

/*
    Name: 循环队列 
    Copyright: 
    Author: yifi 
    Date: 23/03/17 15:12
    Description: 
*/
#include<bits/stdc++.h>
#include <iostream>
using namespace std;


template <class T>
class CycleQueue{
    private:
        unsigned int m_Size;
        int m_Front;
        int m_Rear;
        T*    m_Data;
    public:
        CycleQueue(unsigned size)
                            :m_Size(size),
                             m_Front(0),
                             m_Rear(0)
        {
            m_Data = new T[size];
        }
        ~CycleQueue()
        {
            delete[] m_Data;
        }
        bool IsFull()
        {
            return m_Front == (m_Rear + 1)%m_Size;
        }
        bool IsEmpty()
        {
            return m_Front == m_Rear; 
        }
        void Push(T ele)throw(bad_exception)
        {
            if (IsFull())
            {
                throw bad_exception();
            }
            m_Data[m_Rear] = ele;
            m_Rear = (m_Rear + 1)%m_Size;
        }
        T Pop()throw(bad_exception)
        {
            if (IsEmpty())
            {
                throw bad_exception();
            }
            T Temp = m_Data[m_Front];
            m_Front = (m_Front + 1)%m_Size;
            return Temp;
        }
    
    
};
int main()
{
    CycleQueue<int> q(5);
    q.Push(1);
    q.Push(2);
    q.Push(3);
    q.Push(4);
    for (int i = 0; i < 4 ; i++)
        cout << q.Pop() << endl;
    q.Push(5);
    q.Push(5);
    q.Push(5);
    cout << q.Pop() << endl;
    cout << q.Pop() << endl;
    cout << q.Pop() << endl;
    cout << q.Pop() << endl;
    return 0;
} 

 

posted on 2017-03-23 15:24  yifi  阅读(166)  评论(0编辑  收藏  举报

导航