Created
July 23, 2020 19:34
-
-
Save deleteman/c1f83d8cbdc1b28ed3c71ab84867751b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module.exports = class Stack { | |
data = [] | |
maxSize | |
constructor(initialData, maxSize = -1) { | |
this.data = Array.isArray(initialData) ? initialData : (typeof initialData == "undefined" ? [] : [initialData]) | |
this.maxSize = maxSize | |
} | |
isFull() { | |
return this.maxSize != -1 ? (this.data.length == this.maxSize) : false | |
} | |
isEmpty() { | |
return this.data.length == 0 | |
} | |
add(item) { | |
if(this.isFull()) { | |
return false | |
} | |
this.data.push(item) | |
} | |
*generator() { | |
while(!this.isEmpty()) { | |
yield this.data.pop() | |
} | |
} | |
pop() { | |
const { value, done } = this.generator().next() | |
if(done) return false | |
return value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment