1 / 26

Chapter 6

Queue. Chapter 6. Learning Objectives. Describe the behavior of a queue. Enumerate the primary operations supported by a queue. Learn about the UNIX print queue, the main commands that con be issued to it, and how these commands may be mapped to the operations of the queue data structure.

girene
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queue Chapter 6

  2. Learning Objectives • Describe the behavior of a queue. • Enumerate the primary operations supported by a queue. • Learn about the UNIX print queue, the main commands that con be issued to it, and how these commands may be mapped to the operations of the queue data structure. • Understand the public interface of a queue class in Java and the running times of its methods.

  3. Learning Objectives • Implement a print queue class in Java using the queue class. • Study the implementation of the queue class, and the trade-offs involved in choosing between candidate storage components.

  4. Queue Properties • Queues • Lines in which people “queue” up to be served, in a first come, first served manner. • The typical computing environment queues are used to process requests for service on shared resources. • Printer uses a first-come first-served policy. • First served policy is also known as first in, first out, or FIFO for short.

  5. Queue Properties

  6. Queue Properties • The FIFO policy is applicable only for entries that reach the front of the queue and are then removed. • Entries may leave the queue without reaching the front.

  7. Queue Properties

  8. Floating-Front Design Approach

  9. The Enqueue Operation

  10. The Dequeue Operation

  11. Simple Printer Queue • command enqueues a print job. • checks the status of the printer queue. • First entry is currently being printed (active).

  12. Simple Printer Queue • Deletes the job that is currently active (being printed). • Removes all the jobs.

  13. A Queue Class

  14. A Queue Class • A queue can be considered a specialized (restricted) unordered list. • An efficient implementation would maintain a direct reference to the rear and another to the front. • Enqueue and dequeue can be implemented in O(1) time. • Maintains a running count of the number of entries in the queue. • The methods size and isEmpty can be implmented in O(1).

  15. Queue Class Implementation • Array list • Front and rear are maintained to point to the respective ends of the queue.

  16. Queue Class Implementation

  17. Queue Class Implementation

  18. Queue Class Implementation

  19. Code for Printer Queue

  20. Add new printer job by user Queue<String> printerQueue = new Queue<String>(); Scanner s = new Scanner(System.in); String fileName; System.out.println("Enter printer job file name"); fileName=s.next(); printerQueue.enqueue(fileName);

  21. Remove active printer job fileName= printerQueue.dequeue(); System.out.println(fileName+" is deleted");

  22. Remove all printer jobs printerQueue.clear();

  23. Find number of printer jobs System.out.println("number of print jobs is: "+printerQueue.size());

  24. Print active printer job System.out.println("active print job is: "+printerQueue.first());

  25. Print printer queue status fileName = printerQueue.first(); while(fileName != null) { System.out.println(fileName); fileName = printerQueue.next(); }

  26. The Whole program package printerapplication; import java.util.Scanner; public class PrinterApplication { public static void main(String[] args) { Queue<String> printerQueue = new Queue<String>(); boolean run = true; while(run) {System.out.println("To add new printer job press 1"); System.out.println("To remove active printer job press 2"); System.out.println("To remove all printer jobs press 3"); System.out.println("To find number of printer jobs press 4"); System.out.println("To print active printer job press 5"); System.out.println("To print printer queue status press 6"); System.out.println("To exit press 7"); Scanner s = new Scanner(System.in); String fileName; int choice = s.nextInt(); switch(choice) { case 1: // code to add new printer job then break case 2:// code to remove active printer job then break case 3: // code to remove all printer jobs then break case 4:// code to find number of printer jobs then break case 5:// code to print active printer job then break case 6:// code to print printer queue status then break case 7: run = false; break; }}}}

More Related