|
| 1 | +# JavaScript Stack 알고리즘 |
| 2 | + |
| 3 | +## 1. 스택의 기본 구현 |
| 4 | +```javascript |
| 5 | +class Stack { |
| 6 | + constructor() { |
| 7 | + this.items = []; |
| 8 | + this.count = 0; |
| 9 | + } |
| 10 | + |
| 11 | + // 요소 추가 |
| 12 | + push(element) { |
| 13 | + this.items[this.count] = element; |
| 14 | + this.count++; |
| 15 | + return this.count - 1; |
| 16 | + } |
| 17 | + |
| 18 | + // 요소 제거 |
| 19 | + pop() { |
| 20 | + if(this.count === 0) return undefined; |
| 21 | + |
| 22 | + this.count--; |
| 23 | + const result = this.items[this.count]; |
| 24 | + delete this.items[this.count]; |
| 25 | + return result; |
| 26 | + } |
| 27 | + |
| 28 | + // 최상단 요소 확인 |
| 29 | + peek() { |
| 30 | + return this.items[this.count - 1]; |
| 31 | + } |
| 32 | + |
| 33 | + // 스택이 비어있는지 확인 |
| 34 | + isEmpty() { |
| 35 | + return this.count === 0; |
| 36 | + } |
| 37 | + |
| 38 | + // 스택 크기 반환 |
| 39 | + size() { |
| 40 | + return this.count; |
| 41 | + } |
| 42 | + |
| 43 | + // 스택 초기화 |
| 44 | + clear() { |
| 45 | + this.items = []; |
| 46 | + this.count = 0; |
| 47 | + return this; |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## 2. 스택을 활용한 알고리즘 예시 |
| 53 | + |
| 54 | +### 2.1 괄호 매칭 확인 |
| 55 | +```javascript |
| 56 | +function isValidParentheses(str) { |
| 57 | + const stack = []; |
| 58 | + const brackets = { |
| 59 | + ')': '(', |
| 60 | + '}': '{', |
| 61 | + ']': '[' |
| 62 | + }; |
| 63 | + |
| 64 | + for (let char of str) { |
| 65 | + if (!brackets[char]) { |
| 66 | + stack.push(char); |
| 67 | + } else { |
| 68 | + if (stack.pop() !== brackets[char]) return false; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return stack.length === 0; |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### 2.2 후위 표기식 계산 |
| 77 | +```javascript |
| 78 | +function evaluatePostfix(expr) { |
| 79 | + const stack = []; |
| 80 | + |
| 81 | + for(let token of expr.split(' ')) { |
| 82 | + if(!isNaN(token)) { |
| 83 | + stack.push(Number(token)); |
| 84 | + } else { |
| 85 | + const b = stack.pop(); |
| 86 | + const a = stack.pop(); |
| 87 | + |
| 88 | + switch(token) { |
| 89 | + case '+': stack.push(a + b); break; |
| 90 | + case '-': stack.push(a - b); break; |
| 91 | + case '*': stack.push(a * b); break; |
| 92 | + case '/': stack.push(a / b); break; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return stack[0]; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### 2.3 히스토리 관리 (실행 취소/재실행) |
| 102 | +```javascript |
| 103 | +class UndoRedoStack { |
| 104 | + constructor() { |
| 105 | + this.undoStack = []; |
| 106 | + this.redoStack = []; |
| 107 | + } |
| 108 | + |
| 109 | + // 작업 수행 |
| 110 | + doAction(action) { |
| 111 | + this.undoStack.push(action); |
| 112 | + this.redoStack = []; // 새 작업 시 redo 스택 초기화 |
| 113 | + } |
| 114 | + |
| 115 | + // 실행 취소 |
| 116 | + undo() { |
| 117 | + if (this.undoStack.length === 0) return null; |
| 118 | + const action = this.undoStack.pop(); |
| 119 | + this.redoStack.push(action); |
| 120 | + return action; |
| 121 | + } |
| 122 | + |
| 123 | + // 재실행 |
| 124 | + redo() { |
| 125 | + if (this.redoStack.length === 0) return null; |
| 126 | + const action = this.redoStack.pop(); |
| 127 | + this.undoStack.push(action); |
| 128 | + return action; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## 3. 시간 복잡도 |
| 134 | + |
| 135 | +### 기본 연산 |
| 136 | +- Push: O(1) |
| 137 | +- Pop: O(1) |
| 138 | +- Peek: O(1) |
| 139 | +- isEmpty: O(1) |
| 140 | +- Size: O(1) |
| 141 | + |
| 142 | +### 메모리 사용 |
| 143 | +- 공간 복잡도: O(n), n은 스택에 저장된 요소의 수 |
| 144 | + |
| 145 | +## 4. 활용 사례 |
| 146 | + |
| 147 | +### 4.1 함수 호출 스택 |
| 148 | +```javascript |
| 149 | +function first() { |
| 150 | + second(); |
| 151 | + console.log('first'); |
| 152 | +} |
| 153 | + |
| 154 | +function second() { |
| 155 | + third(); |
| 156 | + console.log('second'); |
| 157 | +} |
| 158 | + |
| 159 | +function third() { |
| 160 | + console.log('third'); |
| 161 | +} |
| 162 | + |
| 163 | +first(); |
| 164 | +// 출력: |
| 165 | +// third |
| 166 | +// second |
| 167 | +// first |
| 168 | +``` |
| 169 | + |
| 170 | +### 4.2 웹 브라우저 방문 기록 |
| 171 | +```javascript |
| 172 | +class BrowserHistory { |
| 173 | + constructor() { |
| 174 | + this.history = []; |
| 175 | + this.current = -1; |
| 176 | + } |
| 177 | + |
| 178 | + visit(url) { |
| 179 | + this.current++; |
| 180 | + this.history[this.current] = url; |
| 181 | + this.history.length = this.current + 1; |
| 182 | + } |
| 183 | + |
| 184 | + back() { |
| 185 | + if (this.current > 0) { |
| 186 | + this.current--; |
| 187 | + return this.history[this.current]; |
| 188 | + } |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + forward() { |
| 193 | + if (this.current < this.history.length - 1) { |
| 194 | + this.current++; |
| 195 | + return this.history[this.current]; |
| 196 | + } |
| 197 | + return null; |
| 198 | + } |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## 5. 주의사항 |
| 203 | + |
| 204 | +1. 메모리 관리 |
| 205 | + - 스택 크기 제한 고려 |
| 206 | + - 메모리 누수 방지를 위한 참조 해제 |
| 207 | + |
| 208 | +2. 에러 처리 |
| 209 | + - 빈 스택에서의 pop 연산 처리 |
| 210 | + - 스택 오버플로우 방지 |
| 211 | + |
| 212 | +3. 성능 최적화 |
| 213 | + - 배열 크기 동적 조정 최소화 |
| 214 | + - 불필요한 복사 연산 방지 |
0 commit comments