码迷,mamicode.com
首页 > Windows程序 > 详细

622 CircularQueue C#

时间:2018-12-02 12:58:55      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:ast   into   str   success   ini   class   turn   param   element   

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();
 */

622 CircularQueue C#

标签:ast   into   str   success   ini   class   turn   param   element   

原文地址:https://www.cnblogs.com/feipeng8848/p/10052935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!