Advertisement
BrewBarred

Edited queue implementation (remove from tail + test script)

Mar 17th, 2025
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. from printDoc import printDoc
  2.  
  3.  
  4. class printList:
  5.     class Node:
  6.         # Constructor
  7.         def __init__(self, doc):
  8.             #
  9.             self.document = doc
  10.             # head of the list
  11.             self.next = None
  12.  
  13.     def __init__(self):
  14.         # the maximum number of documents that can be in the queue
  15.         self.MAX_SIZE = 5
  16.         # the current size of the queue
  17.         self.queue_size = 0
  18.         # the first document in the queue
  19.         self.head = None
  20.         # the last document in the queue
  21.         self.tail = None
  22.  
  23.     # Insert a print request in the queue
  24.     def queueInsert(self, doc):
  25.         # create a node that can be added to the queue using the passed doc
  26.         new_node = printList.Node(doc)
  27.         # if the queue is empty, start a queue
  28.         if self.head is None:
  29.             self.head = new_node
  30.         elif self.tail is None:
  31.             self.tail = new_node
  32.         # else, the queue is full and the tail request should be replaced
  33.         elif self.queue_size >= self.MAX_SIZE:
  34.             print("!!!!!!Attention: Overwrite!!!!!!")
  35.             self.tail.next = new_node
  36.  
  37.         # increment request count to easily find tail of queue
  38.         self.queue_size = self.queue_size + 1
  39.         print(f"Inserted a request in the queue from {new_node.document.getSender()}\n"
  40.               f"Number of requests in the queue {self.queue_size}")
  41.  
  42.         # return the new_node incase it is needed
  43.         return new_node
  44.  
  45.     # Method to print the head of the list
  46.     def queuePrint(self, printerID):
  47.         # Only print if there is a node in the list
  48.         if self.head is not None:
  49.             currNode = self.head
  50.             print(":::::")
  51.             print(f"Printer {printerID} Printing the request from Machine ID: {currNode.document.getSender()} {currNode.document.getStr()}")
  52.             print(":::::")
  53.             # Once printed, remove the node from the queue
  54.             self.head = self.head.next
  55.             self.queue_size -= 1
  56.  
  57.     # Print the contents of the entire list ---for debugging ---
  58.     # Doesn't remove any nodes from the list
  59.     def queuePrintAll(self):
  60.         currNode = self.head
  61.  
  62.         print("LinkedList:", end=" ")
  63.  
  64.         # Traverse through the LinkedList
  65.         while currNode is not None:
  66.             # Print the data at current node
  67.             print(currNode.document.getStr(), end=" ")
  68.  
  69.             # Go to next node
  70.             currNode = currNode.next
  71.         print()
  72.  
  73. # allows devs to test this the output of this queue when this script is directly executed
  74. if __name__ == "__main__":
  75.     # create a print queue to track the documents being printed
  76.     queue = printList()
  77.     # make 6 documents with unique string content and ids for testing purposes
  78.     for i in range(1, 7):
  79.         # create a new printable document
  80.         doc = printDoc(f"Test Machine {i}", i)
  81.         # attempt to add this document into the queue
  82.         queue.queueInsert(doc)
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement