풀이
import sys
N=int(sys.stdin.readline())
deque=[]
for i in range(N):
words=sys.stdin.readline().split()
if words[0]=='push_front':
deque.insert(0,words[1])
elif words[0]=='push_back':
deque.append(words[1])
elif words[0]=='pop_front':
if deque:
print(deque.pop(0))
else:
print(-1)
elif words[0]=='pop_back':
if deque:
print(deque.pop())
else:
print(-1)
elif words[0]=='size':
print(len(deque))
elif words[0]=='empty':
if deque:
print(0)
else:
print(1)
elif words[0]=='front':
if deque:
print(deque[0])
else:
print(-1)
elif words[0]=='back':
if deque:
print(deque[-1])
else:
print(-1)
'학과공부 > 자료구조' 카테고리의 다른 글
[백준] 1874번 - 스택 수열 (By Python) (0) | 2022.07.26 |
---|---|
[백준] 9012번 - 괄호 (By Python) (0) | 2022.07.24 |
[백준] 10845번 - 큐 (By Python) (0) | 2022.07.24 |
[백준] 10828번 - 스택 (By Python) (0) | 2022.07.24 |