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.
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() */
structMyCircularQueue{queue:Vec<i32>,left:usize,right:usize,capacity:usize,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implMyCircularQueue{fnnew(k:i32)->Self{letk=kasusize;Self{queue:vec![0;k],left:0,right:0,capacity:k,}}fnen_queue(&mutself,value:i32)->bool{ifself.is_full(){returnfalse;}self.queue[self.right%self.capacity]=value;self.right+=1;true}fnde_queue(&mutself)->bool{ifself.is_empty(){returnfalse;}self.left+=1;true}fnfront(&self)->i32{ifself.is_empty(){return-1;}self.queue[self.left%self.capacity]}fnrear(&self)->i32{ifself.is_empty(){return-1;}self.queue[(self.right-1)%self.capacity]}fnis_empty(&self)->bool{self.right-self.left==0}fnis_full(&self)->bool{self.right-self.left==self.capacity}}