deque : 스택 + 큐의 장점을 모두 채택한 것
데이터를 넣고 빼는 속도가 리스트 자료형에 비해 효율적, queue 라이브러리를 이용하는 것보다 간단함.
<참고 문서>
https://docs.python.org/ko/3/library/collections.html?highlight=deque#collections.deque
[삽입]
append(x)
appendleft(x)
insert(i, x)
[제거]
pop()
popleft()
remove(value)
<참고 코드>
이것이 취업을 위한 코딩 테스트다 with 파이썬 - 5.2py
https://github.com/ndb796/python-for-coding-test/blob/master/5/2.py
from collections import deque
# 큐(Queue) 구현을 위해 deque 라이브러리 사용
queue = deque()
# 삽입(5) - 삽입(2) - 삽입(3) - 삽입(7) - 삭제() - 삽입(1) - 삽입(4) - 삭제()
queue.append(5)
queue.append(2)
queue.append(3)
queue.append(7)
queue.popleft()
queue.append(1)
queue.append(4)
queue.popleft()
print(queue) # 먼저 들어온 순서대로 출력
queue.reverse() # 다음 출력을 위해 역순으로 바꾸기
print(queue) # 나중에 들어온 원소부터 출력
'Problem Solving > 이론' 카테고리의 다른 글
Python:: 정렬 - 힙 정렬 (0) | 2022.03.15 |
---|---|
Python:: 정렬 - 퀵 정렬 (0) | 2022.03.15 |
Python:: 정렬 - 삽입 정렬 (0) | 2022.03.15 |
Python:: 정렬 - 선택 정렬 (1) | 2022.03.15 |
Python:: 탐색 알고리즘 DFS/BFS (0) | 2022.02.17 |