Repository navigation
Expand file tree
/
Copy pathstack.html
More file actions
77 lines (69 loc) · 3.19 KB
/
Copy pathstack.html
File metadata and controls
77 lines (69 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>실습: 스택 | 자료구조</title>
<link rel="stylesheet" href="../css/common.css">
</head>
<body>
<header class="header">
<h1>실습: 스택 (접시 쌓기)</h1>
<p class="subtitle">맨 위에만 넣고, 맨 위에서만 꺼내기</p>
<div id="theme-switcher-wrap"></div>
</header>
<main class="practice-page">
<a href="../index.html" class="back">← 목차</a>
<a href="../lectures/02-3-스택.html" class="back">II단원 3. 스택 (강의)</a>
<div class="practice-explanation" style="background: var(--callout-etymology-bg); border-left: 4px solid var(--callout-etymology-border); padding: 0.75rem 1rem; margin: 0.75rem 0; border-radius: 0 8px 8px 0;">
<p><strong>스택</strong> = 맨 위에서만 넣고 뺌. <strong>LIFO</strong>(나중에 넣은 게 먼저 나옴). 연산: push → <code>list.append(값)</code>, pop → <code>list.pop()</code>. <strong>응용</strong> 메뉴 뒤로가기, DFS(깊이 우선), 괄호 짝 맞추기.</p>
</div>
<p>접시를 쌓는 것처럼 생각해 보세요. 값을 입력하고 <strong>넣기(Push)</strong>를 누르면 맨 위에 쌓이고, <strong>꺼내기(Pop)</strong>를 누르면 <strong>맨 위에 있는 것만</strong> 나와요. 나중에 넣은 게 먼저 나오는지 확인해 보세요.</p>
<div class="controls">
<input type="text" id="inputVal" placeholder="넣을 값 (예: A)" maxlength="6" aria-label="스택에 넣을 값">
<button type="button" id="btnPush">넣기 (Push)</button>
<button type="button" id="btnPop">꺼내기 (Pop)</button>
</div>
<div class="viz" id="viz" role="status" aria-live="polite">
[ 맨 위 ] — 비어 있음
</div>
<p class="hint">방금 꺼낸 값: <strong id="lastPop">—</strong> (꺼내기를 누르면 여기에 표시돼요)</p>
</main>
<script>
(function () {
var stack = [];
var viz = document.getElementById('viz');
var lastPop = document.getElementById('lastPop');
var inputVal = document.getElementById('inputVal');
function render() {
if (stack.length === 0) {
viz.innerHTML = '[ 맨 위 ] — 비어 있음';
return;
}
var items = stack.slice().reverse().map(function (x, i) {
return '<div class="stack-viz-item">' + (i === 0 ? '← 맨 위 ' : '') + x + '</div>';
}).join('');
viz.innerHTML = '<div class="viz-caption">맨 위 ↑</div>' + items;
}
document.getElementById('btnPush').addEventListener('click', function () {
var v = inputVal.value.trim();
if (v === '') return;
stack.push(v);
inputVal.value = '';
render();
});
document.getElementById('btnPop').addEventListener('click', function () {
if (stack.length === 0) {
lastPop.textContent = '(비어 있음)';
return;
}
var v = stack.pop();
lastPop.textContent = v;
render();
});
render();
})();
</script>
<script src="../js/theme.js"></script>
</body>
</html>