Created
May 25, 2017 13:29
-
-
Save galenweber/738241429f4cf8f1915fc20e95e15be7 to your computer and use it in GitHub Desktop.
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 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