设计循环队列

你的实现应该支持如下操作:

  • MyCircularQueue(k): 构造器,设置队列长度为 k 。
  • Front: 从队首获取元素。如果队列为空,返回 -1 。
  • Rear: 获取队尾元素。如果队列为空,返回 -1 。
  • enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。
  • deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。
  • isEmpty(): 检查循环队列是否为空。
  • isFull(): 检查循环队列是否已满。

Java 实现

class MyCircularQueue {

    /** Initialize your data structure here. Set the size of the queue to be k. */
    private int[] q;
    private int head;
    private int tail;
    public MyCircularQueue(int k) {
        q = new int[k];
        head = -1;
        tail = -1;
    }
    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    public boolean enQueue(int value) {
        if(isFull() == true){
            return false;
        }
        if(tail == q.length-1){
            tail = 0;
        }else if(head == -1 && tail == -1){
            head++;
            tail++;
        }else{
            tail++;
        }
        q[tail] = value;
        return true;
    }
    
   /** Delete an element from the circular queue. Return true if the operation is successful. */
    public boolean deQueue() {
        if(isEmpty() == true){
            return false;
        }
        if(head == q.length-1){
            head = 0;
        }else{
            head++;
        }
        if(head == tail+1 || (tail==q.length-1 && head==0)){
            head = -1;
            tail = -1;
        }
        return true;
        
    }
    
    /** Get the front item from the queue. */
    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return q[head];
    }
    
    /** Get the last item from the queue. */
    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return q[tail];
    }

    /** Checks whether the circular queue is empty or not. */
    public boolean isEmpty() {
        if(head == -1){
            return true;
        }
        return false;
    }
    
    /** Checks whether the circular queue is full or not. */
    public boolean isFull() {
        if(head==0 && tail== q.length-1){
            return true;
        }else if(head == tail + 1){
            return true;
        }else{
            return false;
        }
    }
}

/**
 * Your MyCircularQueue object will be instantiated and called as such:
 * MyCircularQueue obj = new MyCircularQueue(k);
 * boolean param_1 = obj.enQueue(value);
 * boolean param_2 = obj.deQueue();
 * int param_3 = obj.Front();
 * int param_4 = obj.Rear();
 * boolean param_5 = obj.isEmpty();
 * boolean param_6 = obj.isFull();
 */

示例:

MyCircularQueue circularQueue = new MycircularQueue(3); // 设置长度为 3

circularQueue.enQueue(1);  // 返回 true

circularQueue.enQueue(2);  // 返回 true

circularQueue.enQueue(3);  // 返回 true

circularQueue.enQueue(4);  // 返回 false,队列已满

circularQueue.Rear();  // 返回 3

circularQueue.isFull();  // 返回 true

circularQueue.deQueue();  // 返回 true

circularQueue.enQueue(4);  // 返回 true

circularQueue.Rear();  // 返回 4
 

你可能感兴趣的:(Java,Leetcode,Leetcode,造轮子,循环队列,队列,Java)