Created
May 7, 2020 12:51
-
-
Save electricessence/2f342c3274bd84ef449128f543a44851 to your computer and use it in GitHub Desktop.
Simplest JavaScript Queue (best performance) single linked list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queue | |
{ | |
constructor() | |
{ | |
this.count = 0; | |
this.tail = this.root = { | |
next:null | |
}; | |
} | |
enqueue(value) { | |
const newTail = { value: value } | |
this.tail.next = newTail; | |
this.tail = newTail; | |
this.count++; | |
} | |
dequeue() { | |
const n = this.root.next; | |
if(!n) return undefined; | |
this.root.next = n.next; | |
this.count--; | |
if(n.next) n.next = null; | |
else this.tail = this.root; | |
return n.value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment