プリンタタスク用キューの実装
直前の動画では、キューがプリンタのタスク管理など、さまざまな用途に使えることを学びました。
この演習では、簡易的なプリンタ用キューを表す 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.____.____())