SIGN IN SIGN UP

Algorithms and Data Structures implemented in JavaScript for beginners, following best practices.

0 0 0 JavaScript
2017-09-28 10:52:12 -04:00
/* Queue
* A Queue is a data structure that allows you to add an element to the end of
* a list and remove the item at the front. A queue follows a "First In First Out"
* system, where the first item to enter the queue is the first to be removed. This
* implementation uses an array to store the queue.
*/
2020-05-03 09:05:12 +02:00
// Functions: enqueue, dequeue, peek, view, length
2017-09-28 10:52:12 -04:00
2020-10-31 12:03:11 +05:30
const Queue = (function () {
2018-03-30 16:04:27 +02:00
// constructor
2020-05-03 09:05:12 +02:00
function Queue () {
// This is the array representation of the queue
this.queue = []
2018-03-30 16:03:13 +02:00
}
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
// methods
// Add a value to the end of the queue
2018-03-30 16:03:13 +02:00
Queue.prototype.enqueue = function (item) {
2020-10-27 02:59:07 +06:00
this.queue.push(item)
2020-05-03 09:05:12 +02:00
}
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
// Removes the value at the front of the queue
2018-03-30 16:03:13 +02:00
Queue.prototype.dequeue = function () {
if (this.queue.length === 0) {
throw new Error('Queue is Empty')
2017-09-28 10:52:12 -04:00
}
2020-10-31 12:03:11 +05:30
const result = this.queue[0]
2020-05-03 09:05:12 +02:00
this.queue.splice(0, 1) // remove the item at position 0 from the array
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
return result
}
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
// Return the length of the queue
2018-03-30 16:03:13 +02:00
Queue.prototype.length = function () {
2020-05-03 09:05:12 +02:00
return this.queue.length
}
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
// Return the item at the front of the queue
2018-03-30 16:03:13 +02:00
Queue.prototype.peek = function () {
2020-05-03 09:05:12 +02:00
return this.queue[0]
}
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
// List all the items in the queue
2018-03-30 16:03:13 +02:00
Queue.prototype.view = function () {
2020-05-03 09:05:12 +02:00
console.log(this.queue)
}
2018-03-30 16:03:13 +02:00
2020-05-03 09:05:12 +02:00
return Queue
}())
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
// Implementation
2020-10-27 02:59:07 +06:00
const myQueue = new Queue()
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
myQueue.enqueue(1)
myQueue.enqueue(5)
myQueue.enqueue(76)
myQueue.enqueue(69)
myQueue.enqueue(32)
myQueue.enqueue(54)
2017-09-28 10:52:12 -04:00
2020-05-03 09:05:12 +02:00
myQueue.view()
2017-09-28 10:52:12 -04:00
2020-10-27 02:59:07 +06:00
console.log(`Length: ${myQueue.length()}`)
console.log(`Front item: ${myQueue.peek()}`)
console.log(`Removed ${myQueue.dequeue()} from front.`)
console.log(`New front item: ${myQueue.peek()}`)
console.log(`Removed ${myQueue.dequeue()} from front.`)
console.log(`New front item: ${myQueue.peek()}`)
2020-05-03 09:05:12 +02:00
myQueue.enqueue(55)
console.log('Inserted 55')
2020-10-27 02:59:07 +06:00
console.log(`New front item: ${myQueue.peek()}`)
2017-09-28 10:52:12 -04:00
2020-10-31 12:03:11 +05:30
for (let i = 0; i < 5; i++) {
2020-05-03 09:05:12 +02:00
myQueue.dequeue()
myQueue.view()
2017-09-28 10:52:12 -04:00
}
2020-10-31 12:03:11 +05:30
// console.log(myQueue.dequeue()); // throws exception!