2020-10-02 22:15:29 +05:30
|
|
|
|
// Circular Queues offer a quick to store FIFO data with a maximum size.
|
|
|
|
|
|
// Conserves memory as we only store up to our capacity
|
|
|
|
|
|
// It is opposed to a queue which could continue to grow if input outpaces output
|
|
|
|
|
|
// Doesn’t use dynamic memory so No memory leaks
|
|
|
|
|
|
|
|
|
|
|
|
class CircularQueue {
|
2023-10-03 23:08:19 +02:00
|
|
|
|
constructor(maxLength) {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
this.queue = []
|
|
|
|
|
|
this.front = 0
|
|
|
|
|
|
this.rear = 0
|
|
|
|
|
|
this.maxLength = maxLength
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ADD ELEMENTS TO QUEUE
|
2023-10-03 23:08:19 +02:00
|
|
|
|
enqueue(value) {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
if (this.checkOverflow()) return
|
|
|
|
|
|
if (this.checkEmpty()) {
|
|
|
|
|
|
this.front += 1
|
|
|
|
|
|
this.rear += 1
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (this.rear === this.maxLength) {
|
|
|
|
|
|
this.rear = 1
|
|
|
|
|
|
} else this.rear += 1
|
|
|
|
|
|
}
|
|
|
|
|
|
this.queue[this.rear] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// REMOVES ELEMENTS
|
2023-10-03 23:08:19 +02:00
|
|
|
|
dequeue() {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
if (this.checkEmpty()) {
|
2021-10-10 16:11:06 +02:00
|
|
|
|
// UNDERFLOW
|
2020-10-02 22:15:29 +05:30
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
const y = this.queue[this.front]
|
|
|
|
|
|
this.queue[this.front] = '*'
|
2021-10-21 22:59:56 +05:30
|
|
|
|
if (!this.checkSingleelement()) {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
if (this.front === this.maxLength) this.front = 1
|
|
|
|
|
|
else {
|
|
|
|
|
|
this.front += 1
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return y // Returns the removed element and replaces it with a star
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// checks if the queue is empty or not
|
2023-10-03 23:08:19 +02:00
|
|
|
|
checkEmpty() {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
if (this.front === 0 && this.rear === 0) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-03 23:08:19 +02:00
|
|
|
|
checkSingleelement() {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
if (this.front === this.rear && this.rear !== 0) {
|
|
|
|
|
|
this.front = this.rear = 0
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks if max capacity of queue has been reached or not
|
2023-10-03 23:08:19 +02:00
|
|
|
|
checkOverflow() {
|
|
|
|
|
|
if (
|
|
|
|
|
|
(this.front === 1 && this.rear === this.maxLength) ||
|
|
|
|
|
|
this.front === this.rear + 1
|
|
|
|
|
|
) {
|
2021-10-10 16:11:06 +02:00
|
|
|
|
// CIRCULAR QUEUE OVERFLOW
|
2020-10-02 22:15:29 +05:30
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 16:11:06 +02:00
|
|
|
|
// Prints the entire array ('*' represents blank space)
|
2023-10-03 23:08:19 +02:00
|
|
|
|
display(output = (value) => console.log(value)) {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
for (let index = 1; index < this.queue.length; index++) {
|
2021-10-10 16:11:06 +02:00
|
|
|
|
output(this.queue[index])
|
2020-10-02 22:15:29 +05:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Displays the length of queue
|
2023-10-03 23:08:19 +02:00
|
|
|
|
length() {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
return this.queue.length - 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Display the top most value of queue
|
2023-10-03 23:08:19 +02:00
|
|
|
|
peek() {
|
2020-10-02 22:15:29 +05:30
|
|
|
|
return this.queue[this.front]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 16:11:06 +02:00
|
|
|
|
export { CircularQueue }
|