stack1 [자료구조] Stack 직접 구현하기 stack은 후입선출(LIFO)의 자료 구조이다. 즉, 마지막에 넣은 데이터가 가장 먼저 꺼내진다. [구현하기] class Stack { private stack: Array = []; private max = 0; private len = 0; constructor(max: number) { this.max = max; } top(): T { return this.stack[this.len - 1]; } pop(): Stack { if (this.len === 0) throw new Error('stack is empty'); this.stack.splice(this.len - 1, 1); this.len--; return this; } push(value: T): Stack { if (this.len.. 2021. 3. 28. 이전 1 다음