Skip to content

Instantly share code, notes, and snippets.

@galenweber
Created May 25, 2017 13:56
Show Gist options
  • Save galenweber/fca363a8b4d9d48030a57982aac45e5d to your computer and use it in GitHub Desktop.
Save galenweber/fca363a8b4d9d48030a57982aac45e5d to your computer and use it in GitHub Desktop.
class Queue {
constructor() {
this.body = {};
this.minimum;
}
enqueue(val) {
const recurse = function recurse(obj) {
if (obj.above === undefined) {
obj.val = val;
obj.above = {}
} else {
recurse(obj.above)
}
}
recurse(this.body)
if (this.minimum===undefined || val<this.minimum) this.minimum = val;
return this.count();
}
dequeue() {
const bottom = this.body.val;
this.body = this.body.above || {};
return bottom;
}
count() {
const recurse = function recurse(obj) {
if (obj.val === undefined) return 0;
return 1 + recurse(obj.above);
}
return recurse(this.body);
}
min() {
return this.minimum;
}
}
const myQueue = new Queue();
myQueue.enqueue(12);
myQueue.enqueue(15);
myQueue.enqueue(17);
myQueue.dequeue()
myQueue.dequeue()
myQueue.min(); // 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment