Skip to content

Instantly share code, notes, and snippets.

@hk-skit
Last active January 27, 2019 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hk-skit/c742e424523cb3f3ea02826c6c58b266 to your computer and use it in GitHub Desktop.
Save hk-skit/c742e424523cb3f3ea02826c6c58b266 to your computer and use it in GitHub Desktop.
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