# 0622-design-circular-queue Try it on leetcode ## Description

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Implement the MyCircularQueue class:

You must solve the problem without using the built-in queue data structure in your programming language. 

 

Example 1:

Input
["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"]
[[3], [1], [2], [3], [4], [], [], [], [4], []]
Output
[null, true, true, true, false, 3, true, true, true, 4]

Explanation
MyCircularQueue myCircularQueue = new MyCircularQueue(3);
myCircularQueue.enQueue(1); // return True
myCircularQueue.enQueue(2); // return True
myCircularQueue.enQueue(3); // return True
myCircularQueue.enQueue(4); // return False
myCircularQueue.Rear();     // return 3
myCircularQueue.isFull();   // return True
myCircularQueue.deQueue();  // return True
myCircularQueue.enQueue(4); // return True
myCircularQueue.Rear();     // return 4

 

Constraints:

## Solution(Python) ```Python class MyCircularQueue: def __init__(self, k: int): self.front = 0 self.arr = [-1] * k self.capsize = k self.size = 0 def enQueue(self, value: int) -> bool: if self.size >= self.capsize: return False rear = (self.front + self.size) % self.capsize self.arr[rear] = value self.size += 1 return True def deQueue(self) -> bool: if self.size == 0: return False res = self.arr[self.front] self.front = (self.front + 1) % self.capsize self.size -= 1 return True def Front(self) -> int: if self.size == 0: return -1 return self.arr[self.front] def Rear(self) -> int: if self.size == 0: return -1 rear = (self.front + self.size - 1) % self.capsize return self.arr[rear] def isEmpty(self) -> bool: return self.size == 0 def isFull(self) -> bool: return self.size == self.capsize # Your MyCircularQueue object will be instantiated and called as such: # obj = MyCircularQueue(k) # param_1 = obj.enQueue(value) # param_2 = obj.deQueue() # param_3 = obj.Front() # param_4 = obj.Rear() # param_5 = obj.isEmpty() # param_6 = obj.isFull() ```