import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
arr = deque()
for _ in range(N):
command = input().split()
if command[0] == 'push':
arr.append(int(command[1]))
elif command[0] == 'pop':
if arr:
print(arr.popleft())
else:
print(-1)
elif command[0] == 'size':
print(len(arr))
elif command[0] == 'empty':
if not arr:
print(1)
else:
print(0)
elif command[0] == 'front':
if arr:
print(arr[0])
else:
print(-1)
elif command[0] == 'back':
if arr:
print(arr[-1])
else:
print(-1)
선입선출(FIFO)의 자료구조
( 출처: 큐(자료구조) 나무위키 )
먼저 들어오는 데이터가 먼저 나가게 되는 자료구조이다.
파이썬에서 Stack
은 일반 배열을 사용하지만 Queue
은 deque
를 사용하면 시간 효율성을 높일 수 있다.
from collections import deque
arr = deque() // Queue 선언
arr.append(1) // [1]
arr.append(2) // [1, 2]
arr.appendleft(0) // [0, 1, 2]
arr.pop() // [0, 1]
arr.popleft() // [1]