[알고리즘] 백준10866 덱

CHOI IN HO·2023년 12월 6일
0

코딩테스트

목록 보기
6/74

정답

from collections import deque
import sys
N = int(input())
list = deque()
for i in range(N):
a = sys.stdin.readline().split()
if a[0] == 'push_front':
list.insert(0, a[1])
elif a[0] == 'push_back':
list.append(a[1])
elif a[0] == 'pop_front':
if list:
print(list[0])
list.popleft()
else:
print(-1)
elif a[0] == 'pop_back':
if list:
print(list[len(list)-1])
list.pop()
else:
print(-1)
elif a[0] == 'size':
print(len(list))
elif a[0] == 'empty':
if list:
print(0)
else:
print(1)
elif a[0] == 'front':
if list:
print(list[0])
else:
print(-1)
elif a[0] == 'back':
if list:
print(list[len(list)-1])
else:
print(-1)

profile
개발자기 되기 위해선 무엇이든!

0개의 댓글