https://www.acmicpc.net/problem/10866
import sys
from collections import deque
d=deque()
n=int(sys.stdin.readline())
for i in range(n):
inp=sys.stdin.readline().split()
if inp[0]=="push_front":
d.appendleft(inp[1])
elif inp[0]=="push_back":
d.append(inp[1])
elif inp[0] == "pop_front":
if d.__len__() == 0: print(-1)
else:print(d.popleft())
elif inp[0] == "pop_back":
if d.__len__() == 0: print(-1)
else: print(d.pop())
elif inp[0] == "size":
print(d.__len__())
elif inp[0] == "empty":
if d.__len__()==0: print(1)
else: print(0)
elif inp[0] == "front":
if d.__len__()==0: print(-1)
else: print(d[0])
elif inp[0] == "back":
if d.__len__()==0: print(-1)
else: print(d[-1])
위 명령어를 순서대로 실행하면 큐에는 아래그림처럼 담기게 된다.
front는 리스트 형태를 예로 들면 인덱스[0]에서 작업하고
back은 인덱스[-1]에서 작업합니다.
extend와 extendleft는 원소 추가시 reverse되므로 사용않습니다
'자기개발👨💻 > 코딩 알고리즘' 카테고리의 다른 글
[python] 백준10816 숫자 카드2 (0) | 2021.07.12 |
---|---|
[python] 백준11866 요세푸스문제 0 (0) | 2021.07.12 |
[python] 백준2164 카드2 (0) | 2021.07.08 |
[python] 백준2839 설탕 배달 (0) | 2021.07.06 |
[python] 백준 4344 평균은 넘겠지 (0) | 2021.07.06 |
소수 구하기 (0) | 2021.07.01 |