首页 > 其他 > 详细

循环队列

时间:2017-03-23 16:20:15      阅读:103      评论:0      收藏:0      [点我收藏+]
/*
    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;
} 

 

循环队列

原文:http://www.cnblogs.com/yifi/p/6605391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!