import sys
from collections import deque
input = sys.stdin.readline
n = int(input())
q = deque()
for _ in range(n):
command = input().strip().split()
if command[0] == 'push':
q.append(command[1])
elif command[0] == 'pop':
if q:
print(q.popleft())
else:
print(-1)
elif command[0] == 'size':
print(len(q))
elif command[0] == 'empty':
if not q:
print(1)
else:
print(0)
elif command[0] == 'front':
if q:
print(q[0])
else:
print(-1)
elif command[0] == 'back':
if q:
print(q[-1])
else:
print(-1)
파이썬에서 큐를 나타내는 방법이 list와 deque를 쓰는 두가지 방법이 있다.
deque가 연산 속도가 더 빠르다고 하니 이거를 써서 풀어봤다.
풀면서 안 사실
- 큐는 LIFO니까 pop할 때, popleft()를 써야함 (아무생각 없이 pop() 썼다가 틀렸다고 나와서 뭔가 했다..)
- deque에는 size()와 empty() 함수가 없다! 왜일까...?