Files
Fahim Faisaal e05b4432d8 merge: Improved the complexity of dequeue O(n) to O(1) (#1005)
* feat: improved memoize function

used Map instead of object & used the JSON.stringfy method for generate a valid string as a key

* docs: modified documentation

* style: format with standard

* docs: modified stringify doc

* refactor: remove two repetition implementation

* feat: added validation, test codes

* chore: remove useless words

* feat: added types for jest

* chore: added link box

* feat: added new validation test casses & methods

* style: formated with standard

* feat: added parse method & test cases

* docs: added js docs

* chore: added default import export

* feat: imporved algorithm via replace method

* test: added two test cases

* feat: added jest type for suggestions

* feat: added `reduceRight` & `trim` method

* chore: added helper variable

* feat: added new rotation option

* Revert "chore: added helper variable"

This reverts commit 489544da0a3d479910fbea020d3be3d0d10681bf.

* remove: yarn lock

* chore: fix grammer

* feat: used replace method & added test case

* feat: remove revert

* chore: added new line

* feat: updated the Queue array to linkedlist DS

* chore: fixed grammer

* resolve: removed capacity related codes, & updated test cases

* feat: added length dicrease code on dequeue
2022-05-06 21:50:55 +05:30

115 lines
2.1 KiB
JavaScript

/* 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.
*/
class Queue {
#size
constructor () {
this.head = null
this.tail = null
this.#size = 0
return Object.seal(this)
}
get length () {
return this.#size
}
/**
* @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')
}
const firstData = this.peekFirst()
this.head = this.head.next
if (!this.head) {
this.tail = null
}
this.#size--
return firstData
}
/**
* @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
}
/**
* @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
}
/**
* @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
}
/**
* @description - Return is queue empty or not
* @returns {boolean}
*/
isEmpty () {
return this.length === 0
}
}
export default Queue