forked from vJechsmayr/JavaScriptAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.js
More file actions
38 lines (37 loc) · 1.12 KB
/
Queue.js
File metadata and controls
38 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Language: Javascript
//Author: Uddesh Jain
//Github: https://github.com/UddeshJain
class Queue {
constructor() {
this._storage = {}
this._length = 0
this._headIndex = 0
}
/*
* Enqueues a new value at the end of the Queue
* @param {*} value - the value to enqueue
*/
enqueue(value) {
if (value) {
this._storage[this._length + this._headIndex] = value
this._length++
}
}
/*
* Dequeues the value from the begning of the Queue and return it
* @retuen {*} value - the first and oldest value in the Queue
*/
dequeue() {
if (this._length) {
const firstValue = this._storage[this._headIndex]
delete this._storage[this._headIndex]
this._length--
this._headIndex++
return firstValue
}
}
}
const myQueue = new Queue() // Creates new instance
myQueue.enqueue('zero') // Adds the value in the end of Queue
myQueue.dequeue() // Removes the value from first position of Queue
myQueue.enqueue('Two') // Adds the value in the end of Queue