zoukankan      html  css  js  c++  java
  • 循环队列

    /*
        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;
    } 
  • 相关阅读:
    java中的拷贝(二)深克隆
    java中的拷贝(一)
    java类的访问权限
    对象和对象引用
    equals和==
    类与继承(一)
    WebSocket简单介绍
    php获取请求的方式(get/post)
    php 使用jquery实现ajax
    PHP 系统常量及自定义常量
  • 原文地址:https://www.cnblogs.com/yifi/p/6605391.html
Copyright © 2011-2022 走看看