Скачать презентацию Queues The Queue ADT q q The Скачать презентацию Queues The Queue ADT q q The

0b9ca65b0637d03a4163acb889cdb7ba.ppt

  • Количество слайдов: 19

Queues Queues

The Queue ADT q q The Queue ADT stores arbitrary q objects Insertions and The Queue ADT q q The Queue ADT stores arbitrary q objects Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front of the queue Main queue operations: n n enqueue(object): inserts an element at the end of the queue q object dequeue(): removes and returns the element at the front of the queue © 2013 Goodrich, Tamassia, Goldwasser Queues Auxiliary queue operations: n n n object first(): returns the element at the front without removing it integer len(): returns the number of elements stored boolean is_empty(): indicates whether no elements are stored Exceptions n Attempting the execution of dequeue or front on an empty queue throws an Empty. Queue. Exception 2

Example © 2013 Goodrich, Tamassia, Goldwasser Queues 3 Example © 2013 Goodrich, Tamassia, Goldwasser Queues 3

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 © 2013 Goodrich, Tamassia, Goldwasser Queues 4

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 rear f index of the front element r index immediately past the rear element q Array location r is kept empty normal configuration Q 0 1 2 f r wrapped-around configuration Q 0 1 2 r © 2013 Goodrich, Tamassia, Goldwasser f Queues 5

Queue Operations q We use the modulo operator (remainder of division) Algorithm size() return Queue Operations q We use the modulo operator (remainder of division) Algorithm size() return (N f + r) mod N Algorithm is. Empty() return (f = r) Q 0 1 2 f 0 1 2 r r Q © 2013 Goodrich, Tamassia, Goldwasser f Queues 6

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(o) if size() = N 1 then throw Full. Queue. Exception else Q[r] o r (r + 1) mod N Q 0 1 2 f 0 1 2 r r Q © 2013 Goodrich, Tamassia, Goldwasser f Queues 7

Queue Operations (cont. ) q q Operation dequeue throws an exception if the queue Queue Operations (cont. ) q q Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Algorithm dequeue() if is. Empty() then throw Empty. Queue. Exception else o Q[f] f (f + 1) mod N return o Q 0 1 2 f 0 1 2 r r Q © 2013 Goodrich, Tamassia, Goldwasser f Queues 8

Queue in Python q Use the following three instance variables: n _data: is a Queue in Python q Use the following three instance variables: n _data: is a reference to a list instance with a fixed capacity. n _size: is an integer representing the current number of elements stored in the queue (as opposed to the length of the data list). n _front: is an integer that represents the index within data of the first element of the queue (assuming the queue is not empty). © 2013 Goodrich, Tamassia, Goldwasser Queues 9

Queue in Python (1/3) Queue in Python (1/3)

Queue in Python (2/3) Queue in Python (2/3)

Queue in Python (3/3) Queue in Python (3/3)

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 = Q. dequeue() Service element e Q. enqueue(e) Queue Dequeue Enqueue Shared Service © 2013 Goodrich, Tamassia, Goldwasser Queues 13

Double-Ended Queues q q queue-like data structure that supports insertion and deletion at both Double-Ended Queues q q queue-like data structure that supports insertion and deletion at both the front and the back of the queue. Such a structure is called a doubleended queue, or deque, which is usually pronounced “deck” n q to avoid confusion with the dequeue method of the regular queue ADT, which is pronounced like the abbreviation “D. Q. ” An implementation of a deque class is available in Python’s standard collections module.

Deque in Python (1/3) Deque in Python (1/3)

Deque in Python (2/3) Deque in Python (2/3)

Deque in Python (3/3) Deque in Python (3/3)

Summary: Stack, Queue, Deque Summary: Stack, Queue, Deque