410 likes | 429 Views
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.
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.
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.
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.
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).
6.2 UNIX Print Queue • Deletes the job that is currently active (being printed). • Removes all the jobs.
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.
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).
6.4 A PrintQueue Class Using Queue • toString and equals are both declared public. • These methods override their Object class counterparts. (dynamic binding the object type will determine which method to exec.) • Ex: • Class A: Base • Class B : subclass of A • A and B have p method • A a = new A(); • B b = new B(); • a = b; • a.p(); method p of class B will be exec,
6.5 Queue Class Implementation • Array list • Front and rear are maintained to point to the respective ends of the queue.
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
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.
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.
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.
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.
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.
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.