Last active
January 27, 2019 11:27
-
-
Save hk-skit/c742e424523cb3f3ea02826c6c58b266 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
class Stack { | |
constructor() { | |
this.list = new LinkedList(); | |
} | |
get isEmpty() { | |
return this.list.isEmpty; | |
} | |
/** | |
* Iterator function. | |
* | |
* @returns | |
* @memberof Stack | |
*/ | |
[Symbol.iterator]() { | |
return this.list[Symbol.iterator](); | |
} | |
push(value) { | |
this.list.addHead(value); | |
return this; | |
} | |
pop() { | |
if (this.isEmpty) { | |
throw new Error('STACK_UNDERFLOW'); | |
} | |
return this.list.removeHead(); | |
} | |
} | |
const stack = [1,2,3,4,5,6].reduce((stack, value) => stack.push(value),new Stack()); | |
console.log(...stack); // 6 5 4 3 2 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment