|
| 1 | +/** |
| 2 | + * 622. Design Circular Queue |
| 3 | + * https://leetcode.com/problems/design-circular-queue/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * Design your implementation of the circular queue. The circular queue is a linear data structure |
| 7 | + * in which the operations are performed based on FIFO (First In First Out) principle, and the |
| 8 | + * last position is connected back to the first position to make a circle. It is also called |
| 9 | + * "Ring Buffer". |
| 10 | + * |
| 11 | + * One of the benefits of the circular queue is that we can make use of the spaces in front of |
| 12 | + * the queue. In a normal queue, once the queue becomes full, we cannot insert the next element |
| 13 | + * even if there is a space in front of the queue. But using the circular queue, we can use the |
| 14 | + * space to store new values. |
| 15 | + * |
| 16 | + * Implement the MyCircularQueue class: |
| 17 | + * - MyCircularQueue(k) Initializes the object with the size of the queue to be k. |
| 18 | + * - int Front() Gets the front item from the queue. If the queue is empty, return -1. |
| 19 | + * - int Rear() Gets the last item from the queue. If the queue is empty, return -1. |
| 20 | + * - boolean enQueue(int value) Inserts an element into the circular queue. Return true if the |
| 21 | + * operation is successful. |
| 22 | + * - boolean deQueue() Deletes an element from the circular queue. Return true if the operation |
| 23 | + * is successful. |
| 24 | + * - boolean isEmpty() Checks whether the circular queue is empty or not. |
| 25 | + * - boolean isFull() Checks whether the circular queue is full or not. |
| 26 | + * |
| 27 | + * You must solve the problem without using the built-in queue data structure in your programming |
| 28 | + * language. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {number} k |
| 33 | + */ |
| 34 | +var MyCircularQueue = function(k) { |
| 35 | + this.queue = new Array(k); |
| 36 | + this.size = k; |
| 37 | + this.front = -1; |
| 38 | + this.rear = -1; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * @param {number} value |
| 43 | + * @return {boolean} |
| 44 | + */ |
| 45 | +MyCircularQueue.prototype.enQueue = function(value) { |
| 46 | + if (this.isFull()) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + if (this.isEmpty()) { |
| 50 | + this.front = 0; |
| 51 | + } |
| 52 | + this.rear = (this.rear + 1) % this.size; |
| 53 | + this.queue[this.rear] = value; |
| 54 | + return true; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * @return {boolean} |
| 59 | + */ |
| 60 | +MyCircularQueue.prototype.deQueue = function() { |
| 61 | + if (this.isEmpty()) { |
| 62 | + return false; |
| 63 | + } else if (this.front === this.rear) { |
| 64 | + this.front = -1; |
| 65 | + this.rear = -1; |
| 66 | + } else { |
| 67 | + this.front = (this.front + 1) % this.size; |
| 68 | + } |
| 69 | + return true; |
| 70 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * @return {number} |
| 74 | + */ |
| 75 | +MyCircularQueue.prototype.Front = function() { |
| 76 | + return this.isEmpty() ? -1 : this.queue[this.front]; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * @return {number} |
| 81 | + */ |
| 82 | +MyCircularQueue.prototype.Rear = function() { |
| 83 | + return this.isEmpty() ? -1 : this.queue[this.rear]; |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
| 87 | + * @return {boolean} |
| 88 | + */ |
| 89 | +MyCircularQueue.prototype.isEmpty = function() { |
| 90 | + return this.front === -1; |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * @return {boolean} |
| 95 | + */ |
| 96 | +MyCircularQueue.prototype.isFull = function() { |
| 97 | + return (this.rear + 1) % this.size === this.front; |
| 98 | +}; |
0 commit comments