파이썬

5장 자료구조 2_ 리스트 vs 스택 vs 큐

Mara7 2022. 5. 14.
LIST

리스트를 스택으로 사용하기 

스택 : last-in, first-out

삽입 : append() : 리스트 인덱스 마지막 위치에 값 삽입

삭제 : pop() : 리스트 인덱스의 마지막 위치 값 삭제

 

리스트를 큐로 사용하기

큐 : first-in, first-out

비효율적임 , 리스트 앞에 덧붙이거나, 앞에서 꺼내는 작업은 느림.

from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")           # Terry arrives
queue.append("Graham")          # Graham arrives
queue.popleft()                 # The first to arrive now leaves

queue.popleft()                 # The second to arrive now leaves

queue                           # Remaining queue in order of arrival

 

반응형
LIST

'파이썬' 카테고리의 다른 글

파일 읽기 쓰기  (0) 2022.06.30
7. 입력과 출력  (0) 2022.06.28
22.05.29. todolist 복습  (0) 2022.05.29
5장 자료구조 3_리스트 컴프리헨션  (0) 2022.05.15
5장_자료 구조  (0) 2022.05.14

댓글