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 FIFO (First In First Out)
* system, where the first item to enter the queue is the first to be removed,
* All these operation complexities are O(1).
* This implementation following the linked list structure.
*/
2017-09-28 10:52:12 -04:00
class Queue {
#size
constructor() {
this.head = null
this.tail = null
this.#size = 0
return Object.seal(this)
2018-03-30 16:03:13 +02:00
}
2017-09-28 10:52:12 -04:00
get length() {
return this.#size
2020-05-03 09:05:12 +02:00
}
2017-09-28 10:52:12 -04:00
/**
* @description - Add a value to the end of the queue
* @param {*} data
* @returns {number} - The current size of queue
*/
enqueue(data) {
const node = { data, next: null }
if (!this.head && !this.tail) {
this.head = node
this.tail = node
} else {
this.tail.next = node
this.tail = node
}
return ++this.#size
}
/**
* @description - Removes the value at the front of the queue
* @returns {*} - The first data of the queue
*/
dequeue() {
if (this.isEmpty()) {
throw new Error('Queue is Empty')
2017-09-28 10:52:12 -04:00
}
const firstData = this.peekFirst()
this.head = this.head.next
if (!this.head) {
this.tail = null
}
this.#size--
return firstData
2020-05-03 09:05:12 +02:00
}
2017-09-28 10:52:12 -04:00
/**
* @description - Return the item at the front of the queue
* @returns {*}
*/
peekFirst() {
if (this.isEmpty()) {
throw new Error('Queue is Empty')
}
return this.head.data
2020-05-03 09:05:12 +02:00
}
2017-09-28 10:52:12 -04:00
/**
* @description - Return the item at the tail of the queue
* @returns {*}
*/
peekLast() {
if (this.isEmpty()) {
throw new Error('Queue is Empty')
}
return this.tail.data
2020-05-03 09:05:12 +02:00
}
2017-09-28 10:52:12 -04:00
/**
* @description - Return the array of Queue
* @returns {Array<*>}
*/
toArray() {
const array = []
let node = this.head
while (node) {
array.push(node.data)
node = node.next
}
return array
2020-05-03 09:05:12 +02:00
}
2018-03-30 16:03:13 +02:00
/**
* @description - Return is queue empty or not
* @returns {boolean}
*/
isEmpty() {
return this.length === 0
}
}
2017-09-28 10:52:12 -04:00
export default Queue