Скачать презентацию Chapter 6 Queue Learning Objectives Скачать презентацию Chapter 6 Queue Learning Objectives

7f5c365378365d0e08c960c084dffe10.ppt

  • Количество слайдов: 26

Chapter 6 Queue Chapter 6 Queue

Learning Objectives ● ● Describe the behavior of a queue. Enumerate the primary operations 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 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 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 Queue Properties

Queue Properties ● The FIFO policy is applicable only for entries that reach the 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.

Queue Properties Queue Properties

Floating-Front Design Approach Floating-Front Design Approach

The Enqueue Operation The Enqueue Operation

The Dequeue Operation The Dequeue Operation

Simple Printer Queue ● command enqueues a print job. ● checks the status of 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 Simple Printer Queue Deletes the job that is currently active (being printed). Removes all the jobs. ●

A Queue Class A Queue Class

A Queue Class ● ● A queue can be considered a specialized (restricted) unordered 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 is. Empty can be implmented in O(1).

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

Queue Class Implementation Queue Class Implementation

Queue Class Implementation Queue Class Implementation

Queue Class Implementation Queue Class Implementation

Code for Printer Queue Code for Printer Queue

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

Remove active printer job file. Name= printer. Queue. dequeue(); System. out. println(file. Name+ Remove active printer job file. Name= printer. Queue. dequeue(); System. out. println(file. Name+" is deleted");

Remove all printer jobs printer. Queue. clear(); Remove all printer jobs printer. Queue. clear();

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

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

Print printer queue status file. Name = printer. Queue. first(); while(file. Name != null) Print printer queue status file. Name = printer. Queue. first(); while(file. Name != null) { System. out. println(file. Name); file. Name = printer. Queue. next(); }

package printerapplication; import java. util. Scanner; The Whole program public class Printer. Application { package printerapplication; import java. util. Scanner; The Whole program public class Printer. Application { public static void main(String[] args) { Queue printer. Queue = new Queue(); 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 printer queue status press 6"); System. out. println("To exit press 7"); Scanner s = new Scanner(System. in); String file. Name; int choice = s. next. Int(); 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 printer queue status then break case 7: run = false; break; }}}}