Implementing a queue for printer tasks
In the last video, you learned that queues can have multiple applications, such as managing the tasks for a printer.
In this exercise, you will implement a class called PrinterTasks(), which will represent a simplified queue for a printer. To do this, you will be provided with the Queue() class that includes the following methods:
enqueue(data): adds an element to the queuedequeue(): removes an element from the queuehas_elements(): checks if the queue has elements. This is the code:
def has_elements(self):
return self.head != None
You will start coding the PrinterTasks() class with its add_document() and print_documents() methods. After that, you will simulate the execution of a program that uses the PrinterTasks() class.
Latihan ini adalah bagian dari kursus
Data Structures and Algorithms in Python
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
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.____.____())