Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Queue.

Similar presentations


Presentation on theme: "Chapter 6 Queue."— Presentation transcript:

1 Chapter 6 Queue

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

15 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).

16 Queue Class Implementation

17 Queue Class Implementation

18 Queue Class Implementation

19 Code for Printer Queue

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

21 Remove active printer job
fileName= printerQueue.removeFirst(); 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.getFirst());

25 Print printer queue status
System.out.println(printerQueue.toString());

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; }}}}


Download ppt "Chapter 6 Queue."

Similar presentations


Ads by Google