Guidelines

λ§ˆμ§€λ§‰μ— λ“€μ–΄κ°„ 데이터가 κ°€μž₯ λ¨Όμ € λ‚˜μ˜€λŠ” μŠ€νƒ(Stack)

μŠ€νƒμ€ λ§ˆμ§€λ§‰μ— λ“€μ–΄κ°„ μš”μ†Œκ°€ κ°€μž₯ λ¨Όμ € λ‚˜μ˜€λŠ” 'ν›„μž…μ„ μΆœ'(Last In First Out, LIFO) λ°©μ‹μ˜ μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.

일상 μƒν™œμ—μ„œ μ±… 더미에 책을 올리고, 책을 κΊΌλ‚Ό λ•ŒλŠ” κ°€μž₯ μœ„μ— μžˆλŠ” 책을 κΊΌλ‚΄λŠ” 것과 같은 λ°©μ‹μž…λ‹ˆλ‹€.

μŠ€νƒμ€ μ›ΉλΈŒλΌμš°μ € λ°©λ¬Έ 기둝, ν•¨μˆ˜μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ(λ³€μˆ˜, λ§€κ°œλ³€μˆ˜, λ°˜ν™˜ μ£Όμ†Œ λ“±) 관리 λ“±μœΌλ‘œ ν™œμš©λ©λ‹ˆλ‹€.


μŠ€νƒμ˜ κΈ°λ³Έ μ—°μ‚°

  • Push: μŠ€νƒμ— μš”μ†Œ(Element)λ₯Ό μΆ”κ°€ν•˜λŠ” μ—°μ‚°μž…λ‹ˆλ‹€.

  • Pop: μŠ€νƒμ—μ„œ λ§ˆμ§€λ§‰μœΌλ‘œ μΆ”κ°€λœ μš”μ†Œλ₯Ό μ œκ±°ν•˜κ³  μ œκ±°ν•œ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

  • Peek/Top: μŠ€νƒμ˜ μ΅œμƒλ‹¨ μš”μ†Œλ₯Ό λ°˜ν™˜ν•˜μ§€λ§Œ μ œκ±°ν•˜μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€.

  • IsEmpty: μŠ€νƒμ΄ λΉ„μ–΄μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” μ—°μ‚°μž…λ‹ˆλ‹€.


μŠ€νƒ κ΅¬ν˜„ μ˜ˆμ‹œ

νŒŒμ΄μ¬μ—μ„œ μŠ€νƒμ„ μ‚¬μš©ν•˜λŠ” κ°„λ‹¨ν•œ μ˜ˆμ‹œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:

μŠ€νƒ μ‚¬μš© μ˜ˆμ‹œ
stack = [] # μŠ€νƒμ— μš”μ†Œ μΆ”κ°€ (Push) stack.append(1) stack.append(2) stack.append(3) # μŠ€νƒμ˜ μ΅œμƒλ‹¨ μš”μ†Œ 확인 (Peek) print(stack[-1]) # 3 좜λ ₯ # μŠ€νƒμ—μ„œ μ΅œμƒλ‹¨ μš”μ†Œ 제거 (Pop) print(stack.pop()) # 3 좜λ ₯ print(stack.pop()) # 2 좜λ ₯

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result