★큐,덱★큐 2[백준 파이썬 18258번]
2022. 11. 3. 09:55
728x90
반응형
import sys
from collections import deque
queue = deque()
N = int(sys.stdin.readline())
B =[]
for i in range(N):
c = list(map(str, sys.stdin.readline().rstrip().split(' ')))
if c[0] == 'push':
queue.append(c[1])
elif c[0] == 'pop':
if len(queue) == 0:
B.append(-1)
else:
B.append(queue.popleft())
elif c[0] == 'size':
B.append(len(queue))
elif c[0] == 'empty' :
if len(queue) == 0:
B.append(1)
else:
B.append(0)
elif c[0] == 'front':
if len(queue) == 0:
B.append(-1)
else:
B.append(queue[0])
elif c[0] == 'back':
if len(queue) == 0:
B.append(-1)
else:
B.append(queue[-1])
for i in B:
print(i)
deque() ==> 덱의 경우
양 옆으로 뺄 수 있다.
popleft() 왼쪽꺼 뺄 수 있는거고 , popright() 오른쪽꺼 뺄 수 있는거다.
728x90
반응형
'Python(백준) > 큐,덱' 카테고리의 다른 글
★str, join 기억하기★VER2.0★요세푸스문제0[백준 파이썬 11866번]★ (0) | 2023.04.24 |
---|---|
[백준 파이썬 11286번]★절댓값 힙 구현하기★heap.heappush(리스트 , (우선순위 비교값_1) , (우선순위 비교값_2)★heap.heappop(리스트) ==> 우선순위에 따른 pop() (1) | 2022.12.31 |
★큐,덱★Ver2.0★카드2[백준 파이썬 2164번] (0) | 2022.12.31 |
[백준 파이썬 17298번]오큰수★시간초과★VER2.0★ (0) | 2022.12.31 |
★큐,덱★라우터[백준 파이썬 15828번] (0) | 2022.11.03 |