ringbuffer 示例

#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 isEmpty()
        {   
            return m_front == m_rear;
        }   

        bool isFull() 
        {   
            return m_front == (m_rear + 1)%m_size;
        }   

        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 tmp = m_data[m_front];
            m_front = (m_front + 1)%m_size;
            return tmp;
        }
};


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;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容