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

    //循环队列 约定:head = -1且tail=-1时为空
    class MyCircularQueue {
    private:
        int* elements;
        int head;
        int tail;
        int size;
    public:
        /* initialize your data structure here, set the size of the queue to be k*/
        MyCircularQueue(int k) {
            elements = new int[k];
            size = k;
            head = tail = -1;
        }
        ~MyCircularQueue()
        {
            delete[] elements;
        }
        /*insert an element into the circular queue.Return true if the operation is successful*/
        bool enQueue(int value)
        {
            if (isFull()) return false;
            if (isEmpty()) head++;
            tail = (tail + 1) % size;
            elements[tail] = value;
        }
    
        bool deQueue()
        {
            if (isEmpty()) return false;
            //约定head=-1&&tail=-1为空
            if (head == tail) head = tail = -1;
            else
                head = (head + 1) % size;
            return true;
        }
    
        /*get the front the item from the queue*/
        int Front()
        {
            if (isEmpty()) return -1;
            return elements[head];
        }
    
        /*get the last item from the queue*/
        int Rear() {
            if (isEmpty()) return -1;
            return elements[tail];
        }
        /*check whether the circular queue is empty or not*/
        //约定head=-1且tail=-1时为空
        bool isEmpty()
        {
            return head == -1 && tail == -1;
        }
    
        /*check whether the circular queue is full or not*/
        bool isFull()
        {
            return (tail + 1) % size == head;
        }
    };
    
    /*your MyCircularQueue object will be instantiated and called as such:*/
    /*
    MyCircularQueue *obj = new MyCircularQueue(k);
    bool param_1 = obj->enQueue(value)
    bool param_2 = obj->deQueue(value)
    int param_3 = obj->Front();
    int param_4 = obj->Rear();
    bool param_5 = obj->isEmpty();
    bool param_6 = obj->isFull();
    */
    怕什么真理无穷,进一寸有一寸的欢喜。---胡适
  • 相关阅读:
    第十四周学习进度总结
    第十三周学习进度总结
    第十二周学习进度总结
    第十一周学习进度总结
    第十周学习进度总结
    第九周学习进度总结
    第八周学习进度总结
    day16-机器学习十讲第一讲
    day15-验证码识别
    day14-卷积网络识别手写数字
  • 原文地址:https://www.cnblogs.com/hujianglang/p/12793536.html
Copyright © 2011-2022 走看看