백준
백준 10866번: 덱
2호0
2021. 9. 30. 16:55
10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import sys
from collections import deque
n = int(input())
dq = deque([])
for i in range(n):
command = list(map(str,sys.stdin.readline().split()))
if command[0] == "push_front":
dq.appendleft(command[1])
elif command[0] == "push_back":
dq.append(command[1])
elif command[0] == "pop_front":
if not dq:
print(-1)
else:
print(dq.popleft())
elif command[0] == "pop_back":
if not dq:
print(-1)
else:
print(dq.pop())
elif command[0] == "size":
print(len(dq))
elif command[0] == "empty":
if not dq:
print(1)
else:
print(0)
elif command[0] == "front":
if not dq:
print(-1)
else:
print(dq[0])
elif command[0] == "back":
if not dq:
print(-1)
else:
print(dq[-1])
|
cs |