Design your implementation of the Circular queue. the circular queue is a linear data structure in which the operations are performed med 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.
Your implementation shocould support following operations:
MyCircularQueue(k)
: Constructor, set the size of the queue to be K.
Front
: Get the front item from the queue. If the queue is empty, return-1.
Rear
: Get the last item from the queue. If the queue is empty, return-1.
enQueue(value)
: Insert an element into the circular queue. Return true if the operation is successful.
deQueue()
: Delete an element from the circular queue. Return true if the operation is successful.
isEmpty()
: Checks whether the circular queue is empty or not.
isFull()
: Checks whether the circular queue is full or not.
Example:
MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3circularQueue.enQueue(1); // return truecircularQueue.enQueue(2); // return truecircularQueue.enQueue(3); // return truecircularQueue.enQueue(4); // return false, the queue is fullcircularQueue.Rear(); // return 3circularQueue.isFull(); // return truecircularQueue.deQueue(); // return truecircularQueue.enQueue(4); // return truecircularQueue.Rear(); // return 4
Note:
- All values will be in the range of [0, 1000].
- The number of operations will be in the range of [1, 1000].
- Please do not use the built-in queue library.
S
[Leetcode] design circular queue design ring queue