[스택 - 10828번] 스택

Jeong Ha Seung·2022년 1월 27일

import sys

n = int(sys.stdin.readline())

stack = []

for _ in range(n):
    word = sys.stdin.readline().split()  # 공백으로 구분하여 입력받기
    order = word[0]

    if order == "push":
        value = word[1]
        stack.append(value)  # 스택에 집어넣기
    elif order == "pop":
        if len(stack) == 0:
            print(-1)  # 스택에 들어있는 정수가 없으면 -1 출력
        else:
            print(stack.pop())
    elif order == "size":
        print(len(stack))  # 스택에 들어있는 정수의 개수
    elif order == "empty":
        if len(stack) == 0:
            print(1)  # 스택이 비어있으면 1
        else:
            print(0)  # 아니면 0
    elif order == "top":
        if len(stack) == 0:
            print(-1)  # 스택에 들어있는 정수가 없으면 -1 출력
        else:
            print(stack[-1])  # 가장 위에 있는 정수 출력
profile
블로그 이전했습니다. https://morethan-haseung-log.vercel.app

0개의 댓글