push()
pop()
top()
isEmpty()
size()
1) 웹 브라우저 방문기록
뒤로 가기를 눌렀을 때, 가장 최근에 접속한 페이지가 나오는 예제입니다.
history_stack = []
# 페이지 방문
history_stack.append('Home Page')
history_stack.append('About Us')
history_stack.append('Contact Us')
# 뒤로 가기
last_page = history_stack.pop()
print(f"'{last_page}' 페이지에서 뒤로 가기")
2) 되돌리기(undo)
가장 최근에 실행되었던 작업 취소하는 예제입니다.
stack = []
# 작업 실행
stack.append('save')
stack.append('delete')
stack.append('edit')
print("실행된 작업:", stack)
# 가장 최근 작업 취소
last_action = stack.pop()
print(f"'{last_action}' 작업 취소됨")
print("현재 스택 상태:", stack)
3) 단어 역순
word = "hello"
stack = []
# 각 문자를 스택에 추가
for char in word:
stack.append(char)
# 역순으로 문자 꺼내기
reversed_word = ''
while stack:
reversed_word += stack.pop()
print("역순 단어:", reversed_word)
참고 :
https://www.geeksforgeeks.org/difference-between-stack-and-queue-data-structures/
https://www.programiz.com/dsa/stack