|
| 1 | +<h2>622. Design Circular Queue</h2><h3>Medium</h3><hr><div><p>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".</p> |
| 2 | + |
| 3 | +<p>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.</p> |
| 4 | + |
| 5 | +<p>Implementation the <code>MyCircularQueue</code> class:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li><code>MyCircularQueue(k)</code> Initializes the object with the size of the queue to be <code>k</code>.</li> |
| 9 | + <li><code>int Front()</code> Gets the front item from the queue. If the queue is empty, return <code>-1</code>.</li> |
| 10 | + <li><code>int Rear()</code> Gets the last item from the queue. If the queue is empty, return <code>-1</code>.</li> |
| 11 | + <li><code>boolean enQueue(int value)</code> Inserts an element into the circular queue. Return <code>true</code> if the operation is successful.</li> |
| 12 | + <li><code>boolean deQueue()</code> Deletes an element from the circular queue. Return <code>true</code> if the operation is successful.</li> |
| 13 | + <li><code>boolean isEmpty()</code> Checks whether the circular queue is empty or not.</li> |
| 14 | + <li><code>boolean isFull()</code> Checks whether the circular queue is full or not.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>You must solve the problem without using the built-in queue data structure in your programming language. </p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong>Example 1:</strong></p> |
| 21 | + |
| 22 | +<pre><strong>Input</strong> |
| 23 | +["MyCircularQueue", "enQueue", "enQueue", "enQueue", "enQueue", "Rear", "isFull", "deQueue", "enQueue", "Rear"] |
| 24 | +[[3], [1], [2], [3], [4], [], [], [], [4], []] |
| 25 | +<strong>Output</strong> |
| 26 | +[null, true, true, true, false, 3, true, true, true, 4] |
| 27 | + |
| 28 | +<strong>Explanation</strong> |
| 29 | +MyCircularQueue myCircularQueue = new MyCircularQueue(3); |
| 30 | +myCircularQueue.enQueue(1); // return True |
| 31 | +myCircularQueue.enQueue(2); // return True |
| 32 | +myCircularQueue.enQueue(3); // return True |
| 33 | +myCircularQueue.enQueue(4); // return False |
| 34 | +myCircularQueue.Rear(); // return 3 |
| 35 | +myCircularQueue.isFull(); // return True |
| 36 | +myCircularQueue.deQueue(); // return True |
| 37 | +myCircularQueue.enQueue(4); // return True |
| 38 | +myCircularQueue.Rear(); // return 4 |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p> </p> |
| 42 | +<p><strong>Constraints:</strong></p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li><code>1 <= k <= 1000</code></li> |
| 46 | + <li><code>0 <= value <= 1000</code></li> |
| 47 | + <li>At most <code>3000</code> calls will be made to <code>enQueue</code>, <code>deQueue</code>, <code>Front</code>, <code>Rear</code>, <code>isEmpty</code>, and <code>isFull</code>.</li> |
| 48 | +</ul> |
| 49 | +</div> |
0 commit comments