BaşlayınÜcretsiz Başlayın

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 queue
  • dequeue(): removes an element from the queue
  • has_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.

Bu egzersiz

Data Structures and Algorithms in Python

kursunun bir parçasıdır
Kursu Görüntüle

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

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.____.____())
Kodu Düzenle ve Çalıştır