[백준 10845 파이썬] - 큐

zsunny·2022년 7월 25일
1

📌 문제

💯 정답

import sys
input = sys.stdin.readline

def push(x):
    q.append(x)

def pop():
    if len(q) == 0:
        return -1
    else:
        return q.pop(0)     # pop()이 아닌 pop(0)으로 앞 데이터 pop 가능

def size():
    return len(q)

def empty():
    if len(q) == 0:
        return 1
    else:
        return 0

def front():
    if len(q) == 0:
        return -1
    else:
        return q[0]

def back():
    if len(q) == 0:
        return -1
    else:
        return q[-1]

n = int(input())
q = []

for _ in range(n):
    order_list = list(input().rstrip().split())
    order = order_list[0]
    if order == "push":
        push(order_list[1])
    elif order == "pop":
        print(pop())
    elif order == "size":
        print(size())
    elif order == "empty":
        print(empty())
    elif order == "front":
        print(front())
    elif order == "back":
        print(back())

📝 설명

• 전에 했던 "10828 스택"과 비슷한 문제이다.
• 큐에서 pop은 가장 위가 아닌 처음에 있는 정수를 빼고 출력해야하므로 pop[0]으로 쓴다.

⭐️ 참고

👉 [백준 10828 파이썬] - 스택 설명 바로가기

profile
매일 성장하는 예비 웹 개발자 🌱

0개의 댓글