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 */
  • 相关阅读:
    一、列表
    正则表达式
    form表单学习
    HTTP场景应用
    fiddler几种功能强大的用法(二)
    在VMW里安装Ghost操作系统遇到的问题及解决的办法
    浮点数值的表示
    补码和补码的计算
    个人主页项目总结
    Todolist项目总结 JavaScript+jQuery
  • 原文地址:https://www.cnblogs.com/ycbeginner/p/10006413.html
Copyright © 2011-2022 走看看