260 likes | 279 Vues
Learn the behavior, operations, implementation, and properties of queues in UNIX, Java, and everyday scenarios. Dive into queue data structures and practical coding examples to enhance your understanding.
E N D
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. • Understand the public interface of a queue class in Java and the running times of its methods.
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.
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.
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.
Simple Printer Queue • command enqueues a print job. • checks the status of the printer queue. • First entry is currently being printed (active).
Simple Printer Queue • Deletes the job that is currently active (being printed). • Removes all the jobs.
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).
Queue Class Implementation • Array list • Front and rear are maintained to point to the respective ends of the queue.
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);
Remove active printer job fileName= printerQueue.dequeue(); System.out.println(fileName+" is deleted");
Remove all printer jobs printerQueue.clear();
Find number of printer jobs System.out.println("number of print jobs is: "+printerQueue.size());
Print active printer job System.out.println("active print job is: "+printerQueue.first());
Print printer queue status fileName = printerQueue.first(); while(fileName != null) { System.out.println(fileName); fileName = printerQueue.next(); }
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; }}}}