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();
 */
原文地址:https://www.cnblogs.com/feipeng8848/p/10052935.html