백준 문제풀이/스택(Stack)

백준 온라인 저지 - 스택(# 10828) 파이썬

Itscool 2022. 2. 24. 01:07

문제설명

기본적인 스택 자료구조를 구현하는 문제입니다. 

 

스택 (# 10828) 파이썬 코드

import sys

n = int(sys.stdin.readline())
stack = []
for i in range(n):
    inp = list(map(str, sys.stdin.readline().rstrip().split()))
    if inp[0] == "push":
        stack.append(inp[1])
    elif inp[0] == "pop":
        if not stack:
            print(-1)
            continue
        print(stack.pop())
    elif inp[0] == "size":
        print(len(stack))
    elif inp[0] == "empty":
        if not stack:
            print(1)
            continue
        print(0)
    elif inp[0] == "top":
        if not stack:
            print(-1)
            continue
        print(stack[-1])