|
| 1 | +// Circular Queues offer a quick to store FIFO data with a maximum size. |
| 2 | +// Conserves memory as we only store up to our capacity |
| 3 | +// It is opposed to a queue which could continue to grow if input outpaces output |
| 4 | +// Doesn’t use dynamic memory so No memory leaks |
| 5 | + |
| 6 | +class CircularQueue { |
| 7 | + constructor (maxLength) { |
| 8 | + this.queue = [] |
| 9 | + this.front = 0 |
| 10 | + this.rear = 0 |
| 11 | + this.maxLength = maxLength |
| 12 | + } |
| 13 | + |
| 14 | + // ADD ELEMENTS TO QUEUE |
| 15 | + enqueue (value) { |
| 16 | + if (this.checkOverflow()) return |
| 17 | + if (this.checkEmpty()) { |
| 18 | + this.front += 1 |
| 19 | + this.rear += 1 |
| 20 | + } else { |
| 21 | + if (this.rear === this.maxLength) { |
| 22 | + this.rear = 1 |
| 23 | + } else this.rear += 1 |
| 24 | + } |
| 25 | + this.queue[this.rear] = value |
| 26 | + } |
| 27 | + |
| 28 | + // REMOVES ELEMENTS |
| 29 | + dequeue () { |
| 30 | + if (this.checkEmpty()) { |
| 31 | + console.log('UNDERFLOW') |
| 32 | + return |
| 33 | + } |
| 34 | + const y = this.queue[this.front] |
| 35 | + this.queue[this.front] = '*' |
| 36 | + if (this.checkSingleelement()) { |
| 37 | + |
| 38 | + } else { |
| 39 | + if (this.front === this.maxLength) this.front = 1 |
| 40 | + else { |
| 41 | + this.front += 1 |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return y // Returns the removed element and replaces it with a star |
| 46 | + } |
| 47 | + |
| 48 | + // checks if the queue is empty or not |
| 49 | + checkEmpty () { |
| 50 | + if (this.front === 0 && this.rear === 0) { |
| 51 | + return true |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + checkSingleelement () { |
| 56 | + if (this.front === this.rear && this.rear !== 0) { |
| 57 | + this.front = this.rear = 0 |
| 58 | + return true |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Checks if max capacity of queue has been reached or not |
| 63 | + checkOverflow () { |
| 64 | + if ((this.front === 1 && this.rear === this.maxLength) || (this.front === this.rear + 1)) { |
| 65 | + console.log('CIRCULAR QUEUE OVERFLOW') |
| 66 | + return true |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Prints the entire array |
| 71 | + display () { |
| 72 | + for (let index = 1; index < this.queue.length; index++) { |
| 73 | + console.log(this.queue[index]) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Displays the length of queue |
| 78 | + length () { |
| 79 | + return this.queue.length - 1 |
| 80 | + } |
| 81 | + |
| 82 | + // Display the top most value of queue |
| 83 | + peek () { |
| 84 | + return this.queue[this.front] |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function main () { |
| 89 | + // Star represents blank space |
| 90 | + const queue = new CircularQueue(6) // Enter Max Length |
| 91 | + queue.enqueue(1) |
| 92 | + queue.enqueue(15) |
| 93 | + queue.enqueue(176) |
| 94 | + queue.enqueue(59) |
| 95 | + queue.enqueue(3) |
| 96 | + queue.enqueue(55) |
| 97 | + |
| 98 | + queue.display() |
| 99 | + |
| 100 | + queue.dequeue() |
| 101 | + queue.dequeue() |
| 102 | + queue.dequeue() |
| 103 | + queue.display() |
| 104 | + |
| 105 | + console.log(queue.peek()) |
| 106 | +} |
| 107 | + |
| 108 | +main() |
0 commit comments