[LeetCode]622. Design circular queue (Java)

Design your loop queue implementation. Circular queue is a linear data structure whose operation performance is based on FIFO (first in first out) principle, and the tail of the queue is connected after the head of the queue to form a loop. It is also called "ring buffer".

One of the benefits of circular queues is that we can use the space previously used by this queue. In a normal queue, once a queue is full, we cannot insert the next element, even if there is still space in front of the queue. But with circular queues, we can use this space to store new values.

Your implementation should support the following operations:

  • MyCircularQueue(k): constructor that sets the queue length to K.
  • Front: get the element from the team leader. If the queue is empty, - 1 is returned.
  • Rear: get the tail element. If the queue is empty, - 1 is returned.
  • enQueue(value): inserts an element into the circular queue. Returns true if successfully inserted.
  • deQueue(): deletes an element from the circular queue. Returns true if the deletion was successful.
  • isEmpty(): check whether the circular queue is empty.
  • isFull(): check whether the circular queue is full.

Tips:

  • All values are in the range of 0 to 1000;
  • The operand will be in the range of 1 to 1000;
  • Please do not use the built-in queue library.
class MyCircularQueue {
    private int[] queue = null;
    private int head, tail;
    private int size = 0;

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

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

Tags: Java leetcode

Posted by dan231 on Thu, 12 May 2022 00:02:06 +0300