Skip to content

Instantly share code, notes, and snippets.

@electricessence
Created May 7, 2020 12:51
Show Gist options
  • Save electricessence/2f342c3274bd84ef449128f543a44851 to your computer and use it in GitHub Desktop.
Save electricessence/2f342c3274bd84ef449128f543a44851 to your computer and use it in GitHub Desktop.
Simplest JavaScript Queue (best performance) single linked list.
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