Skip to content

Instantly share code, notes, and snippets.

@galenweber
Created May 25, 2017 13:29
Show Gist options
  • Save galenweber/738241429f4cf8f1915fc20e95e15be7 to your computer and use it in GitHub Desktop.
Save galenweber/738241429f4cf8f1915fc20e95e15be7 to your computer and use it in GitHub Desktop.
class Stack {
constructor() {
this.body = {};
this.minimum;
}
push(elem) {
this.body = {
val: elem,
below: Object.assign({}, this.body)
};
if (this.minimum===undefined || elem<this.minimum) this.minimum = elem;
return this.count();
}
pop() {
const top = this.body.val;
this.body = this.body.below || {};
return top;
}
count() {
const recurs = function recurs(obj) {
if (!obj.val) return 0;
return 1 + recurs(obj.below);
}
return recurs(this.body);
}
min() {
return this.minimum;
}
}
const myStack = new Stack();
myStack.push(12);
myStack.push(15);
myStack.push(17);
myStack.min(); // 12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment