Guidelines

μ ‘μ‹œμ²˜λŸΌ μŒ“μ•„ μ˜¬λ¦¬λŠ” μŠ€νƒ(Stack)

μŠ€νƒμ€ 데이터λ₯Ό ν…Œμ΄λΈ” μœ„μ˜ μ ‘μ‹œμ²˜λŸΌ μŒ“μ•„ μ˜¬λ¦¬λŠ” μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.

μŒ“μ•„μ˜¬λ¦° μ ‘μ‹œλ₯Ό κΊΌλ‚Ό λ•Œ κ°€μž₯ μœ„μ— μžˆλŠ” μ ‘μ‹œλΆ€ν„° μ°¨λ‘€λ‘œ κΊΌλ‚΄λŠ” κ²ƒμ²˜λŸΌ, μŠ€νƒμ—μ„œλŠ” λ§ˆμ§€λ§‰μ— μΆ”κ°€λœ 데이터가 κ°€μž₯ λ¨Όμ € μ œκ±°λ©λ‹ˆλ‹€.

즉 LIFO(Last In, First Out), "λ§ˆμ§€λ§‰μ— λ“€μ–΄μ˜¨ 것이 κ°€μž₯ λ¨Όμ € λ‚˜κ°„λ‹€"λΌλŠ” κ·œμΉ™μ„ λ”°λ¦…λ‹ˆλ‹€.


μŠ€νƒμ€ μ–΄λ–»κ²Œ ν™œμš©λ˜λ‚˜μš”?

  • μ›Ή λΈŒλΌμš°μ €μ˜ λ’€λ‘œ κ°€κΈ°: λΈŒλΌμš°μ €λŠ” λ°©λ¬Έν•œ νŽ˜μ΄μ§€λ“€μ„ μŠ€νƒμ— μ €μž₯ν•©λ‹ˆλ‹€. λ’€λ‘œ κ°€κΈ° λ²„νŠΌμ„ λˆ„λ₯Ό λ•Œλ§ˆλ‹€ λ§ˆμ§€λ§‰μ— λ°©λ¬Έν•œ νŽ˜μ΄μ§€λΆ€ν„° μ°¨λ‘€λ‘œ 이전 νŽ˜μ΄μ§€λ‘œ λŒμ•„κ°‘λ‹ˆλ‹€.

  • ν…μŠ€νŠΈ μ—λ””ν„°μ˜ μ‹€ν–‰ μ·¨μ†Œ: ν…μŠ€νŠΈ μ—λ””ν„°λŠ” μ‚¬μš©μžκ°€ μž…λ ₯ν•œ ν…μŠ€νŠΈλ₯Ό μŠ€νƒμ— μ €μž₯해두고, μ‹€ν–‰ μ·¨μ†Œ λ²„νŠΌμ„ λˆ„λ₯Ό λ•Œλ§ˆλ‹€ λ§ˆμ§€λ§‰μ— μž…λ ₯ν•œ ν…μŠ€νŠΈλΆ€ν„° μ°¨λ‘€λ‘œ μ‚­μ œν•©λ‹ˆλ‹€.


μŠ€νƒμ˜ μ£Όμš” μ—°μ‚°

μŠ€νƒμ—μ„œλŠ” λͺ‡ 가지 κΈ°λ³Έ 연산을 μ‚¬μš©ν•©λ‹ˆλ‹€.

  • push: μŠ€νƒμ˜ 맨 μœ„μ— μƒˆλ‘œμš΄ 데이터λ₯Ό μΆ”κ°€ν•˜λŠ” μ—°μ‚°μž…λ‹ˆλ‹€.

  • pop: μŠ€νƒμ˜ 맨 μœ„μ— μžˆλŠ” 데이터λ₯Ό 제거 및 λ°˜ν™˜ν•˜λŠ” μ—°μ‚°μž…λ‹ˆλ‹€.

  • peek: μŠ€νƒμ˜ 맨 μœ„μ— μžˆλŠ” 데이터λ₯Ό μ œκ±°ν•˜μ§€ μ•Šκ³  ν™•μΈλ§Œ ν•˜λŠ” μ—°μ‚°μž…λ‹ˆλ‹€.

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


νŒŒμ΄μ¬μ—μ„œμ˜ μŠ€νƒ κ΅¬ν˜„

νŒŒμ΄μ¬μ—μ„œλŠ” 리슀트(List)λ₯Ό μ΄μš©ν•΄ κ°„λ‹¨νžˆ μŠ€νƒμ„ κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

리슀트의 append() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ 데이터λ₯Ό μΆ”κ°€ν•˜κ³ , pop() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ 데이터λ₯Ό μ œκ±°ν•˜λ©΄ μŠ€νƒμ˜ 기본적인 κΈ°λŠ₯을 κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

νŒŒμ΄μ¬μ—μ„œμ˜ μŠ€νƒ κ΅¬ν˜„ 예제
class Stack: # μŠ€νƒ μ΄ˆκΈ°ν™” def __init__(self): self.stack = [] # push μ—°μ‚°: μŠ€νƒμ— 데이터 μΆ”κ°€ def push(self, item): self.stack.append(item) print(f"Push: {item}κ°€ μŠ€νƒμ— μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.") # pop μ—°μ‚°: μŠ€νƒμ—μ„œ 데이터 제거 def pop(self): if not self.is_empty(): item = self.stack.pop() print(f"Pop: {item}κ°€ μŠ€νƒμ—μ„œ μ œκ±°λ˜μ—ˆμŠ΅λ‹ˆλ‹€.") return item else: print("Pop: μŠ€νƒμ΄ λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.") return None # peek μ—°μ‚°: μŠ€νƒμ˜ μ΅œμƒμœ„ κ°’ 확인 def peek(self): if not self.is_empty(): return self.stack[-1] else: print("Peek: μŠ€νƒμ΄ λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.") return None def is_empty(self): return len(self.stack) == 0 # μŠ€νƒ μ‚¬μš© μ˜ˆμ‹œ my_stack = Stack() my_stack.push(1) # Push: 1이 μŠ€νƒμ— μΆ”κ°€ my_stack.push(2) # Push: 2κ°€ μŠ€νƒμ— μΆ”κ°€ print(f"Peek: ν˜„μž¬ μŠ€νƒμ˜ μ΅œμƒμœ„ 값은 {my_stack.peek()}μž…λ‹ˆλ‹€.") my_stack.pop() # Pop: 2κ°€ μŠ€νƒμ—μ„œ 제거 my_stack.pop() # Pop: 1이 μŠ€νƒμ—μ„œ 제거 my_stack.pop() # μŠ€νƒμ΄ λΉ„μ–΄ μžˆμ–΄ μ œκ±°ν•  수 μ—†μŒ

μ •λ¦¬ν•˜λ©΄?

  • μŠ€νƒμ€ LIFO(Last In, First Out) ꡬ쑰λ₯Ό λ”°λ₯΄λŠ” μžλ£Œκ΅¬μ‘°μž…λ‹ˆλ‹€.

  • μ£Όμš” μ—°μ‚°μœΌλ‘œλŠ” push(μΆ”κ°€), pop(제거), peek(확인), is_empty(λΉ„μ–΄ μžˆλŠ”μ§€ 확인)이 μžˆμŠ΅λ‹ˆλ‹€.

Mission
0 / 1

λΉˆμΉΈμ— κ°€μž₯ μ ν•©ν•œ μŠ€νƒμ˜ 연산은 λ¬΄μ—‡μΌκΉŒμš”?

μŠ€νƒμ˜ 맨 μœ„μ— μƒˆλ‘œμš΄ 데이터λ₯Ό μΆ”κ°€ν•˜λŠ” 연산은 μž…λ‹ˆλ‹€.
push
pop
peek
is_empty

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result