zoukankan      html  css  js  c++  java
  • 622 CircularQueue C#

    public class MyCircularQueue {
    
        int[] Queue=null;
        int _Front = 0;
        int _Rear = 0;
        int Length = 0;
        int Count = 0;
        /** Initialize your data structure here. Set the size of the queue to be k. */
        public MyCircularQueue(int k) {
            Queue = new int[k];
            _Front = 0;
            _Rear = 0;
            Count = 0;
            Length = k;
        }
        
        /** Insert an element into the circular queue. Return true if the operation is successful. */
        public bool EnQueue(int value) {
            if(IsFull())
                return false;
            
            Queue[_Rear] = value;
            
            _Rear++;
            _Rear = _Rear % Length;        
            
            Count++;        
            return true;
        }
        
        /** Delete an element from the circular queue. Return true if the operation is successful. */
        public bool DeQueue() {
            if(IsEmpty())
                return false;        
            
            _Front++;        
            _Front  = _Front % Length;
            Count--;
            return true;
        }
        
        /** Get the front item from the queue. */
        public int Front() {
            if(IsEmpty())
                return -1;
            return Queue[_Front];
        }
        
        /** Get the last item from the queue. */
        public int Rear() {     
            if(IsEmpty())
                return -1;
            int rear = _Rear - 1;
            if(rear == -1)
                rear = rear+Length;        
            return Queue[rear];
        }
        
        /** Checks whether the circular queue is empty or not. */
        public bool IsEmpty() {
            if(Count == 0)
                return true;
            return false;
        }
        
        /** Checks whether the circular queue is full or not. */
        public bool IsFull() {
            if(Count == Length)
                return true;
            return false;
        }
    }
    
    /**
     * 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();
     * int param_3 = obj.Front();
     * int param_4 = obj.Rear();
     * bool param_5 = obj.IsEmpty();
     * bool param_6 = obj.IsFull();
     */
    
  • 相关阅读:
    css 图片的无缝滚动
    有时间研究下这个
    js的类数组对象
    js的this什么时候会出现报错
    js前端分页
    js队列
    js前端处理url中的参数为对象
    随机看的一点代码
    js的callee和caller方法
    js的Object和Function
  • 原文地址:https://www.cnblogs.com/feipeng8848/p/10052935.html
Copyright © 2011-2022 走看看