1 / 15

5.2 Queues

5.2 Queues. Definition: a Queue is a collection of objects that are inserted and removed according to the first-in-first-out (FIFI – LILO) principle.

galya
Download Presentation

5.2 Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 5.2 Queues Queues

  2. Definition: a Queue is a collection of objects that are inserted and removed according to the first-in-first-out(FIFI – LILO) principle. • That is, elements can be inserted at any time, but only the element that has been in the queue the longest (first inserted) can be removed at any time. • Usually, elements enter a queue at the Rear, and are accessed or removed from the Front. Queues

  3. The Queue ADT stores a sequence of arbitrary objects. Insertions and deletions follow the first-in first-out scheme. Insertions are restricted to the end of the sequence (Rear). Accesses and removals are restricted to the first element of the sequence (Front). The Queue ADT (§5.2.1) Queues

  4. The queue abstract data type (ADT) supports the following methods (operations): • Main queue operations: • enqueue(e): inserts an element e at the end of the queue • dequeue(): removes and returns the element at the front of the queue • Auxiliary queue operations: • front(): returns the element at the front without removing it • size(): returns an integer value that indicates the number of elements stored in the queue • isEmpty(): returns a Boolean value that indicates whether the queue is empty • Exceptions: • Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException Queues

  5. Queue Example: The following table shows a series of queue operations and their effects on an initially empty queue Q of integer objects: Queues

  6. Applications of Queues • Direct applications • Waiting lists, theaters, reservation centers,… etc • Access to shared resources (e.g., printer) • Multiprogramming • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures Queues

  7. Queue Interface in Java public interfaceQueue{ public int size(); public boolean isEmpty(); public Object front()throwsEmptyQueueException; public voidenqueue(Object o); public Object dequeue()throwsEmptyQueueException;} • Java interface corresponding to our Queue ADT • Requires the definition of class EmptyQueueException • No corresponding built-in Java class Queues

  8. 5.2.2 Array-Based Queue Implementation a- Simple Array Implementation • Using an array Q, of fixed size N, to store queue elements. • If we let Front is Q[0], and let queue grow from left to right, as in case of Stack, it’s not efficient solution. • It requires moving all queue-elements forward one-array cell, each time we perform dequeue operation. • Such an implementation requires O(n) time to perform dequeue-method, where n is the current elements in queue. Queues

  9. To avoid moving objects once they are placed in Q, and toexecute queue methods in a constant time O(1) Use an array of fixed size N in a circular fashion Define two integer variables to keep track of the front and rear array cells f index of the front element in Q, first nonempty cell of array r index to the next available empty array cell in Q, rear element Array location r is kept empty Q 0 1 2 f r Q 0 1 2 r f b- Using an Array in a Circular Way normal configuration f<r N-1 wrapped-around configuration f>r N-1 Queues

  10. Q 0 1 2 f r Q 0 1 2 r f Queue Operations Algorithmsize() return(N-f +r) mod N AlgorithmisEmpty() return(f=r) AlgorithmFront() ifisEmpty()then throwEmptyQueueException elsereturnQ[f] • Initially, we assign f = r =0, which indicates that the queue is empty. • We use the modulo operator % (remainder of division) N-1 N-1 Queues

  11. Q 0 1 2 f r Q 0 1 2 r f Queue Operations (cont.) Algorithmenqueue(o) ifsize()=N 1then throw FullQueueException else {Q[r] o r(r + 1) mod N } • Operation enqueue throws an exception if the array is full • This exception is implementation-dependent N-1 N-1 Queues

  12. Q 0 1 2 f r Q 0 1 2 r f Queue Operations (cont.) Algorithmdequeue() ifisEmpty()then throw EmptyQueueException else {oQ[f] f(f + 1) mod N returno } • Operation dequeue throws an exception if the queue is empty • This exception is specified in the queue ADT N-1 N-1 Queues

  13. 5.2.3 Implementing Queue with Linked Lists Queues

  14. Queue with a Singly Linked List • We can implement a queue with a singly linked list • The front element is stored at the first node • The rear element is stored at the last node • The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f  elements Linked Lists

  15. The Queue 2 . Service the 3 . Enqueue the 1 . Deque the next element serviced element next element Shared Service Application: Round Robin Schedulers • We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: • e = Q.dequeue() • Service element e • Q.enqueue(e) Queues

More Related