Download presentation
Presentation is loading. Please wait.
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
6.1 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
6.1 Queue Properties
6
6.1 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
6.1 Queue Properties
8
6.2 UNIX Print Queue The UNIX lpr command enqueues a print job.
lpq checks the status of the printer queue. First entry is currently being printed (active).
9
6.2 UNIX Print Queue Deletes the job that is currently active (being printed). Removes the job whose id is jobid. Removes all the jobs.
10
6.3 A Queue Class
11
6.3 A Queue Class
12
6.3 A Queue Class
13
6.3 A Queue Class A queue can be considered a specialized (restricted) unordered list. All methods supported by Queue have their functional counterpart in the List class. Exception:dequeue and postionOf. Enqueue is identical in functionality to add. All other methods have identical names between both classes.
14
6.3 A Queue Class 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
6.4 A PrintQueue Class Using Queue
16
6.4 A PrintQueue Class Using Queue
17
6.4 A PrintQueue Class Using Queue
Job class hierarchy A basic Job class that stands for the type of all objects in the queue. OwnerJob class compares owner names. OwnerIdJob class compares owner names and job ids.
18
6.4 A PrintQueue Class Using Queue
19
6.4 A PrintQueue Class Using Queue
20
6.4 A PrintQueue Class Using Queue
21
6.4 A PrintQueue Class Using Queue
toString and equals are both declared public. These methods override their Object class counterparts.
22
6.4 A PrintQueue Class Using Queue
23
6.4 A PrintQueue Class Using Queue
24
6.4 A PrintQueue Class Using Queue
25
6.4 A PrintQueue Class Using Queue
26
6.5 Queue Class Implementation
Array list Front and rear are maintained to point to the respective ends of the queue.
27
6.5 Queue Class Implementation
28
6.5 Queue Class Implementation
29
6.5 Queue Class Implementation
Every enqueue and dequeue pair results in array location being wasted. Circular Array When the queue wraps back, the rear index becomes less than the front index. If the rear index is less than the front index, then the gap between the rear and front is the unused space. Compute the used space by subtracting this unused space from the length of the array. front - rear -1
30
6.5 Queue Class Implementation
31
6.5 Queue Class Implementation
In an empty queue, the rear index is one less than the front index. If the queue is filled and the rear index keeps advancing until it ends up coming to one position behind the front index, it looks the same as the empty queue. Keeping a count of the number of entries in the queue, starting with 0 when the queue is created resolves this ambiguity.
32
6.5 Queue Class Implementation
33
6.5.2 Design 2: Using Linked List
34
6.5.2 Design 2: Using Linked List
35
6.5.2 Design 2: Using Linked List
36
6.6 Summary The queue data structure implements the First In First Out (FIFO) abstraction. A queue is a linear collection of entires in which, for every pair of entries x and y, if x leaves from the front of the queue before y, then x must have entered the queue before y. An entry may leave a queue before reaching the front. In this case, that entry is not served.
37
6.6 Summary There are two fundamental operations supported by a queue: enqueue and dequeue. A queue class may provide more than just the fundamental enqueue and dequeue operations to facilitate ease of use. A queue may be viewed as a specialized or restricted unordered list. A print queue in UNIX can be implemented using the queue data structure.
38
6.6 Summary Implementing a UNIX print queue using the Queue class requires the Queue clients to build a class hierarchy that will enable the matching of a queue entry against a specific item based on either job id, job owner, or both. If class B extends class A, then any method in B that overrides an inherited method from A cannot be less accessible than the inherited method.
39
6.6 Summary An array list may be used to implement the queue, but this would result in a implementation that is either inefficient in time or wasteful of space usage. A circular array may be used to implement a queue, with the attendant problem of overestimating or underestimating the space requirement associated with the static allocation of an array.
40
6.6 Summary A linked list is better than either an array list or a circular array to implement the queue. When class A reuses an instance of class B as a component, exceptions thrown by methods of B may have to be caught by A in order to reinterpret them for clients of A.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.