컴퓨터 알고리즘

스택 정리(+ 스택으로 풀 수 있는 프로그래밍 문제, 파이썬)

The Ohgorithm 2025. 9. 28. 21:15

 

<스택 설명>

1. FILO, First In Last Out, 선입후출

2. 스택 삽입: Push, 스택에서 꺼내기: Pop

 

<스택 추상 자료형 ADT(abstract data type)>

1. push: 푸시

2. pop: 팝

3. isFull: 가득 찼는지 확인

4. isEmpty: 비었는지 확인

5. top: 최근에 삽입한 데이터의 위치 저장할 변수

 

 

 


 

 

def push(stack, item):
  stack.append(item)
  print('데이터가 추가되었습니다.')

def pop(stack, item):
  stack.pop()
  print('데이터가 삭제되었습니다.')

def peek(stack):
  print(stack[-1])

 

stack = []
stack.append(1)
stack.append(2)
stack.append(3)

top_element = stack.pop()
next_element = stack.pop()
print(top_element, next_element)

stack_size = len(stack)
print(stack_size)

 

 


 

<스택으로 풀 수 있는 프로그래밍 문제>

 

1. 괄호 짝 맞추기

2. 괄호 회전하기

3. 10진수를 2진수로 변환하기

4. 짝지어 제거하기

5. 주식 가격

6. 크레인 인형 뽑기 게임

7. 표 편집