Queues 2014 Goodrich Tamassia Goldwasser Queues 1 The

  • Slides: 15
Download presentation
Queues © 2014 Goodrich, Tamassia, Goldwasser Queues 1

Queues © 2014 Goodrich, Tamassia, Goldwasser Queues 1

The Queue ADT q q q The Queue ADT stores arbitrary objects Insertions and

The Queue ADT q q q The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme (FIFO) Insertions are at the rear of the queue and removals are at the front of the queue © 2014 Goodrich, Tamassia, Goldwasser Queues 2

Main queue operations q enqueue(Q, item) n q inserts an item at the end

Main queue operations q enqueue(Q, item) n q inserts an item at the end of the queue item dequeue(Q) n // insert in the book // delete in the book removes and returns the item at the front of the queue © 2014 Goodrich, Tamassia, Goldwasser Queues 3

Auxiliary queue operations q item first(Q): n q integer size(Q): n q returns the

Auxiliary queue operations q item first(Q): n q integer size(Q): n q returns the number of elements stored boolean is. Empty(Q): n q returns the element at the front without removing it indicates whether no elements are stored Boundary cases: n Attempting the execution of dequeue or first on an empty queue returns null © 2014 Goodrich, Tamassia, Goldwasser Queues 4

Example Operation enqueue(Q, 5) enqueue(Q, 3) dequeue(Q) enqueue(Q, 7) dequeue(Q) first(Q) dequeue(Q) is. Empty(Q)

Example Operation enqueue(Q, 5) enqueue(Q, 3) dequeue(Q) enqueue(Q, 7) dequeue(Q) first(Q) dequeue(Q) is. Empty(Q) enqueue(Q, 9) enqueue(Q, 7) size(Q) enqueue(Q, 3) enqueue(Q, 5) dequeue(Q) – – 5 – 3 7 7 null true – – 2 – – 9 © 2014 Goodrich, Tamassia, Goldwasser Output Q (5) (5, 3) (3, 7) (7) () (9) (9, 7) (9, 7, 3, 5) (7, 3, 5) Queues 5

Applications of Queues q Direct applications n n n q Waiting lists, bureaucracy Access

Applications of Queues q Direct applications n n n q Waiting lists, bureaucracy Access to shared resources (e. g. , printer) Multiprogramming Indirect applications n n Auxiliary data structure for algorithms Component of other data structures © 2014 Goodrich, Tamassia, Goldwasser Queues 6

Array-based Queue q q Use an array of size N in a circular fashion

Array-based Queue q q Use an array of size N in a circular fashion Two variables keep track of the front and size f index of the front element sz number of stored elements q When the queue has fewer than N elements, array location r = (f + sz) mod N is the first empty slot past the rear of the queue normal configuration Q 0 1 2 f r wrapped-around configuration Q 0 1 2 r © 2014 Goodrich, Tamassia, Goldwasser Queues f 7

Why “circular” array? q Why not “regular” array? the first item on the queue

Why “circular” array? q Why not “regular” array? the first item on the queue is always the first item in the array? front rear n 5 3 9 7 © 2014 Goodrich, Tamassia, Goldwasser Queues 8

Can we use a linked list to implement a queue? q What are the

Can we use a linked list to implement a queue? q What are the tradeoffs? © 2014 Goodrich, Tamassia, Goldwasser Queues 9

Queue Operations Algorithm size(Q) return Q->sz Algorithm is. Empty(Q) return (Q->sz == 0) Q

Queue Operations Algorithm size(Q) return Q->sz Algorithm is. Empty(Q) return (Q->sz == 0) Q 0 1 2 f 0 1 2 r r Q © 2014 Goodrich, Tamassia, Goldwasser f Queues 10

Queue Operations (cont. ) q q Operation enqueue throws an exception if the array

Queue Operations (cont. ) q q Operation enqueue throws an exception if the array is full This exception is implementationdependent Algorithm enqueue(Q, item) if size(Q) = N then print error else r (Q->f + Q->sz) mod N Q->data[r] item Q->sz (Q->sz + 1) Q 0 1 2 f 0 1 2 r r Q © 2014 Goodrich, Tamassia, Goldwasser f Queues 11

Queue Operations (cont. ) q Note that operation dequeue returns null if the queue

Queue Operations (cont. ) q Note that operation dequeue returns null if the queue is empty Algorithm dequeue(Q) if is. Empty(Q) then return null else item Q->data[Q->f] Q->f (Q->f + 1) mod N Q->sz (Q->sz - 1) return item Q 0 1 2 f 0 1 2 r r Q © 2014 Goodrich, Tamassia, Goldwasser f Queues 12

Worst-case time complexity N items on the queue Operation “Circular Array” “Normal” Array Linked

Worst-case time complexity N items on the queue Operation “Circular Array” “Normal” Array Linked List (head & tail) enqueue dequeue © 2014 Goodrich, Tamassia, Goldwasser Queues 13

Worst-case time complexity N items on the queue Operation “Circular” Array “Normal” Array Linked

Worst-case time complexity N items on the queue Operation “Circular” Array “Normal” Array Linked List (head & tail) enqueue O(1) dequeue O(1) O(N) O(1) © 2014 Goodrich, Tamassia, Goldwasser Queues 14

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue

Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: q 1. 2. 3. e = dequeue(Q) Service item e enqueue(Q, e) Queue Dequeue Enqueue Shared Service © 2014 Goodrich, Tamassia, Goldwasser Queues 15