Last active
August 29, 2015 14:02
-
-
Save juandopazo/689bc36a4cf5d8994d15 to your computer and use it in GitHub Desktop.
JS Queue
This file contains 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 QueueItem { | |
constructor(value) { | |
this.value = value; | |
this.next = void 0; | |
} | |
} | |
class Queue { | |
constructor() { | |
this.first = void 0; | |
this.last = void 0; | |
} | |
enqueue(value) { | |
var wasEmpty = !this.first; | |
var item = new QueueItem(value); | |
if (wasEmpty) { | |
this.first = item; | |
} else { | |
this.last.next = item; | |
} | |
this.last = item; | |
return wasEmpty; | |
} | |
dequeue() { | |
var first = this.first; | |
if (first) { | |
this.first = first.next; | |
} | |
if (first === this.last) { | |
this.last = void 0; | |
} | |
return first.value; | |
} | |
isEmpty() { | |
return !this.first; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment