Skip to content

Instantly share code, notes, and snippets.

@juandopazo
Last active August 29, 2015 14:02
Show Gist options
  • Save juandopazo/689bc36a4cf5d8994d15 to your computer and use it in GitHub Desktop.
Save juandopazo/689bc36a4cf5d8994d15 to your computer and use it in GitHub Desktop.
JS Queue
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