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:
MyCircularQueue(k) Initializes the object with the size of the queue to be k.
int Front() Gets the front item from the queue. If the queue is empty, return -1.
int Rear() Gets the last item from the queue. If the queue is empty, return -1.
boolean enQueue(int value) Inserts an element into the circular queue. Return true if the operation is successful.
boolean deQueue() Deletes an element from the circular queue. Return true if the operation is successful.
boolean isEmpty() Checks whether the circular queue is empty or not.
boolean isFull() Checks whether the circular queue is full or not.
You must solve the problem without using the built-in queue data structure in your programming language.
At most 3000 calls will be made to enQueue, deQueue, Front, Rear, isEmpty, and isFull.
Solutions
Solution 1: Array Simulation
We can use an array $q$ of length $k$ to simulate a circular queue, with a pointer $\textit{front}$ to record the position of the front element. Initially, the queue is empty, and $\textit{front}$ is $0$. Additionally, we use a variable $\textit{size}$ to record the number of elements in the queue, initially $\textit{size}$ is $0$.
When calling the enQueue method, we first check if the queue is full, i.e., $\textit{size} = k$. If it is full, we return $\textit{false}$. Otherwise, we insert the element at position $(\textit{front} + \textit{size}) \bmod k$, then $\textit{size} = \textit{size} + 1$, indicating that the number of elements in the queue has increased by $1$. Finally, we return $\textit{true}$.
When calling the deQueue method, we first check if the queue is empty, i.e., $\textit{size} = 0$. If it is empty, we return $\textit{false}$. Otherwise, we set $\textit{front} = (\textit{front} + 1) \bmod k$, indicating that the front element has been dequeued, then $\textit{size} = \textit{size} - 1$.
When calling the Front method, we first check if the queue is empty, i.e., $\textit{size} = 0$. If it is empty, we return $-1$. Otherwise, we return $q[\textit{front}]$.
When calling the Rear method, we first check if the queue is empty, i.e., $\textit{size} = 0$. If it is empty, we return $-1$. Otherwise, we return $q[(\textit{front} + \textit{size} - 1) \bmod k]$.
When calling the isEmpty method, we simply check if $\textit{size} = 0$.
When calling the isFull method, we simply check if $\textit{size} = k$.
In terms of time complexity, the above operations all have a time complexity of $O(1)$. The space complexity is $O(k)$.
classMyCircularQueue{privatequeue:number[];privateleft:number;privateright:number;privatecapacity:number;constructor(k:number){this.queue=newArray(k);this.left=0;this.right=0;this.capacity=k;}enQueue(value:number):boolean{if(this.isFull()){returnfalse;}this.queue[this.right%this.capacity]=value;this.right++;returntrue;}deQueue():boolean{if(this.isEmpty()){returnfalse;}this.left++;returntrue;}Front():number{if(this.isEmpty()){return-1;}returnthis.queue[this.left%this.capacity];}Rear():number{if(this.isEmpty()){return-1;}returnthis.queue[(this.right-1)%this.capacity];}isEmpty():boolean{returnthis.right-this.left===0;}isFull():boolean{returnthis.right-this.left===this.capacity;}}/** * Your MyCircularQueue object will be instantiated and called as such: * var obj = new MyCircularQueue(k) * var param_1 = obj.enQueue(value) * var param_2 = obj.deQueue() * var param_3 = obj.Front() * var param_4 = obj.Rear() * var param_5 = obj.isEmpty() * var param_6 = obj.isFull() */