프린터 작업을 위한 큐 구현
이전 영상에서 큐가 프린터 작업 관리처럼 여러 용도로 쓰일 수 있다는 점을 배웠습니다.
이번 연습에서는 프린터용 간단한 큐를 나타내는 PrinterTasks() 클래스를 구현해 보겠습니다. 이를 위해 다음 메서드를 포함한 Queue() 클래스가 제공됩니다:
enqueue(data): 큐에 요소를 추가합니다dequeue(): 큐에서 요소를 제거합니다has_elements(): 큐에 요소가 있는지 확인합니다. 코드는 다음과 같습니다:
def has_elements(self):
return self.head != None
PrinterTasks() 클래스의 add_document()와 print_documents() 메서드부터 코딩을 시작하세요. 이후에는 PrinterTasks() 클래스를 사용하는 프로그램 실행을 시뮬레이션합니다.
이 연습은 강의의 일부입니다
Python으로 배우는 자료구조와 알고리즘
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class PrinterTasks:
def __init__(self):
self.queue = Queue()
def add_document(self, document):
# Add the document to the queue
self.____.____(document)
def print_documents(self):
# Iterate over the queue while it has elements
while self.____.____():
# Remove the document from the queue
print("Printing", self.____.____())