5.2 queues

15
© 2004 Goodrich, Tamassia © 2004 Goodrich, Tamassia © 2004 Goodrich, Tamassia Queues 1 5.2 Queues

Upload: galya

Post on 19-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 1

5.2 Queues

Page 2: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia 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.

Page 3: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 3

The Queue ADT (§5.2.1)

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).

Page 4: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia 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

Page 5: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia 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:

Operation Output Front Q Rear

enqueue(5) - (5)

enqueue(3) - (5,3)

dequeue() 5 (3)

enqueue(7) - (3,7)

dequeue() 3 (7)

front() 7 (7)

dequeue() 7 ()

dequeue() “error” ()

isEmpty() true ()

enqueue(9) - (9)

enqueue(8) - (9,8)

Size() 2 (9,8)

enqueue(3) - (9,8,3)

enqueue(5) - (9,8,3,5 )

dequeue() 9 (8,7,3)

Page 6: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia 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

Page 7: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 7

Queue Interface in Java

Java interface corresponding to our Queue ADTRequires the definition of class EmptyQueueExceptionNo corresponding built-in Java class

public interface Queue {

public int size();

public boolean isEmpty();

public Object front()throws EmptyQueueException;

public void enqueue(Object o);

public Object dequeue() throws EmptyQueueException;

}

Page 8: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 8

a- Simple Array ImplementationUsing 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.

5.2.2 Array-Based Queue Implementation

Page 9: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 9

To avoid moving objects once they are placed in Q, and to execute queue methods in a constant time O(1)Use an array of fixed size N in a circular fashionDefine 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 arrayr index to the next available empty array cell in Q, rear element

Array location r is kept empty

Q0 1 2 rf

normal configuration f<r

Q0 1 2 fr

wrapped-around configuration f>r

b- Using an Array in a Circular Way

N-1

N-1

Page 10: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 10

Queue Operations

Initially, we assign f = r =0, which indicates

that the queue is empty.We use the modulo operator % (remainder of division)

Algorithm size()return (N f + r) mod N

Algorithm isEmpty()return (f r)

Algorithm Front()if isEmpty() then

throw EmptyQueueException else return Q[f]

Q0 1 2 rf

Q0 1 2 fr

N-1

N-1

Page 11: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 11

Queue Operations (cont.)Algorithm enqueue(o)

if size() = N 1 thenthrow FullQueueException

else { Q[r] o r (r + 1) mod N }

Operation enqueue throws an exception if the array is fullThis exception is implementation-dependent

Q0 1 2 rf

Q0 1 2 fr

N-1

N-1

Page 12: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 12

Queue Operations (cont.)

Operation dequeue throws an exception if the queue is emptyThis exception is specified in the queue ADT

Algorithm dequeue()if isEmpty() then

throw EmptyQueueException else

{o Q[f] f (f + 1) mod N return o }

Q0 1 2 rf

Q0 1 2 fr

N-1

N-1

Page 13: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 13

5.2.3 Implementing Queue with Linked Lists

Page 14: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Linked Lists 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

f

r

nodes

elements

Page 15: 5.2  Queues

© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia© 2004 Goodrich, Tamassia Queues 15

Application: Round Robin Schedulers

We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps:

1. e = Q.dequeue()2. Service element e3. Q.enqueue(e)

The Queue

Shared Service

1. Deque the next element

3. Enqueue the serviced element

2 . Service the next element