zoukankan      html  css  js  c++  java
  • C++ 循环队列基本算法实现

    C++ 循环队列基本算法实现

    #ifndef CircleQueue_h
    #define CircleQueue_h
    
    const int QueueSize = 1000;
    template <class T>
    class CircleQueue
    {
    public:
        CircleQueue(){front = rear = 0;}
        void EnQueue(T x);
        T DeQueue();
        T GetFront();
        void SetNull();
        int GetLength();
        bool Empty(){return front == rear ? true : false;}
    private:
        T data[QueueSize];
        int front;
        int rear;
    };
    template <class T>
    void CircleQueue<T>::EnQueue(T x){
        if((rear+1)%QueueSize == front) throw "overflow";
        rear = (rear + 1)%QueueSize; //队尾指向下一个位置
        data[rear] = x;
    }
    template <class T>
    T CircleQueue<T>::DeQueue(){
        if(front == rear) throw "the Queue is null";
        front = (front+1)%QueueSize;
        T x = data[front];
        return x;
    }
    template <class T>
    T CircleQueue<T>::GetFront(){
        if(front == rear) throw "the Queue is null";
        return data[(front +1)%QueueSize];
    }
    template <class T>
    int CircleQueue<T>::GetLength(){
        return (rear-front +QueueSize)%QueueSize;
    }
    template <class T>
    void CircleQueue<T>::SetNull(){
        rear = 0;
        front = 0;
    }
    
    #endif /* CircleQueue_h */
  • 相关阅读:
    bzoj 4017: 小Q的无敌异或
    [TJOI2014] Alice and Bob
    [TJOI2014] 上升子序列
    bzoj 3261: 最大异或和
    bzoj3087: Coci2009 misolovke
    bzoj3521: [Poi2014]Salad Bar
    bzoj4032: [HEOI2015]最短不公共子串
    bzoj1027: [JSOI2007]合金
    bzoj4637: 期望
    bzoj3919: [Baltic2014]portals
  • 原文地址:https://www.cnblogs.com/ycbeginner/p/10006413.html
Copyright © 2011-2022 走看看