MapleStory Finger Point

[자료구조] #2 큐

2021. 5. 30. 01:04Problem Solving/Data Structure

** 큐(Queue) 구조

- 가장 먼저 넣은 데이터를 가장 먼저 꺼낼 수 있는 구조       ex) 음식점에 줄 서서 입장하는 방식

- FIFO(First-In, First-Out) 또는 LILO(Last-In, Last-Out) 방식        cf) 스택 - LIFO(Last-In, First-Out)

 

클릭시 출처로 이동

 

 

 

** 관련 용어

- Enqueue: 큐에 데이터를 넣는 기능

- Dequeue: 큐에 데이터를 꺼내는 기능

- Visualgo 사이트에서 시연해보며 이해하기 (enqueue/dequeue 만 클릭해보며)

 

VisuAlgo - Linked List (Single, Doubly), Stack, Queue, Deque

VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter

visualgo.net

 

 

** 파이썬 queue 라이브러리 활용

- 프로그램에 따라 적합한 자료구조 사용

  1) Queue(): 가장 일반적인 큐 자료구조

  2) LifoQueue(): 나중에 입력된 데이터가 먼저 출력되는 구조(스택과 동일한 구조)

  3) PriorityQueue(): 데이터마다 우선순위를 넣어서, 우선순위가 높은 순으로 데이터를 출력하는 구조


import queue

# 1

data_queue_1 = queue.Queue()

data_queue_1.put('A')

data_queue_1.qsize() # 1

data_queue_1.get() # A

data_queue_1.qsize # 0

 

# 2

data_queue_2 = queue.LifoQueue()

data_queue_2.put('A')

data_queue_2.put('B')

data_queue_2.qsize() # 2

data_queue_2.get() # B

 

# 3

data_queue_3 = queue.PriorityQueue()

data_queue_3.put((10, 'A')) # 튜플 타입

data_queue_3.put((5, 'B'))

data_queue_3.get() # B


 

 

** 큐 활용 예

- 멀티 태스킹을 위한 프로세스 스케쥴링 방식을 구현하기 위해 많이 사용됨 (운영체제)

- 참고: 큐는 특별히 언급되는 장단점이 없음

 

 

 

** 큐의 기능 직접 구현


queue_list = list() # 리스트 변수 사용

 

def enqueue(data):
    queue_list.append(data)
    
def dequeue():
    data = queue_list[0]
    del queue_list[0]
    return data

 

for index in range(10):
    enqueue(index)

 

len(queue_list) # 10

 

dequeue() # 0