Queues
Queues
Queues
Briana B. Morrison
Adapted from Alan Eugenio
Topics
Define Queue
APIs
Applications
Radix Sort
Simulation
Implementation
Array based
Circular
Deques
Priority Queues
Queues
Queues
The Queue
A
push A
fro nt
back
fro nt
back
fro nt
fro nt
back
fro nt
back
C
back
B
C
push B
push C
pop A
A Queue is a FIFO
(First in First Out)
Data Structure.
Elements are inserted
in the Rear of the
queue and are
removed at the Front.
pop B
Queues
METHOD INTERFACES
FOR THE
queue CLASS
Queues
CLASS queue
Constructor
<queue>
Operations
<queue>
queue();
Create an empty queue.
CLASS queue
CLASS queue
Operations
<queue>
Queues
CLASS queue
Operations
<queue>
Queues
10
11
12
Queues
13
deque?
OK
list?
OK
vector?
Queues
14
15
Queues
16
Queues
17
m in iQ u e u e < in t> m in iQ ; // d e c la r e a n e m p ty q u e u e
Q u e u e S ta te m e n t
m in iQ .p u s h (1 0 )
Q ueue
L is t S ta te m e n t
q lis t.p u s h _ b a c k (1 0 )
10
fr o n tb a c k
m in iQ .p u s h (2 5 )
10
10
10
fro n t b a c k
q lis t.p u s h _ b a c k (2 5 )
25
fr o n t b a c k
m in iQ .p u s h (5 0 )
L is t
25
fr o n t
fro n t
10
back
fr o n t
r e tu r n q lis t.fr o n t()
m in iQ .p o p ()
50
fr o n t
back
Queues
back
25
50
n = m in iQ .fr o n t() // n = 1 0
25
25
10
50
back
// r e tu r n 1 0
25
50
18
Queues
19
Applications of Queues
Direct applications
Indirect applications
Queues
20
Queues
21
30
91
22
92
35
15
85
Queues
39
22
After Collection: 30 91 92 22 85 15 35 6 39
Pass 1: Take the new sequence and distribute the
cards into bins determined by the 10's digit (10 1).
Final Sequence: 6 15 22 30 35 39 85 91 92
15
22
39
35
30
6
Queues
85
92
91
9
23
Radix Sort
Use an array of queues (or vector of queues) as the buckets
void radixSort (vector<int>& v, int d)
{
int i;
int power = 1;
queue<int> digitQueue[10];
Queues
24
Queues
25
Queues
26
APPLICATION OF QUEUES:
COMPUTER SIMULATION
Queues
27
A SYSTEM IS A COLLECTION OF
INTERACTING PARTS.
A MODEL IS A SIMPLIFICATION OF
A SYSTEM.
THE PURPOSE OF BUILDING A MODEL
IS TO STUDY THE UNDERLYING
SYSTEM.
Queues
28
Queues
29
Queues
30
A
500
D
?
200
500
B
400
C
E
Queues
31
32
System
DEVELOP
VERIFY
Interpretation
Computer
Model
RUN
DECIPHER
Queues
Output
33
Queues
34
35
36
Queues
37
1.
2.
3.
4.
5.
38
Desired output:
39
Design approach:
Queues
40
Queues
41
QUEUE APPLICATION
A SIMULATED CAR WASH
Queues
42
PROBLEM:
GIVEN THE ARRIVAL TIMES AT
SPEEDYS CAR WASH, CALCULATE THE
AVERAGE WAITING TIME PER CAR.
Queues
43
Queues
44
45
46
SYSTEM TEST 1:
8
11
11
13
14
16
16
20
999
Queues
47
TIME
8
11
11
13
14
16
16
18
20
28
38
48
58
68
78
EVENT
WAITING TIME
ARRIVAL
ARRIVAL
ARRIVAL
ARRIVAL
ARRIVAL
ARRIVAL
ARRIVAL (OVERFLOW)
DEPARTURE
0
ARRIVAL
DEPARTURE
7
DEPARTURE
17
DEPARTURE
25
DEPARTURE
34
DEPARTURE
42
Queues 48
DEPARTURE
48
49
Queues
50
Implementing a Queue
Array based
51
Array-based Queue
normal configuration
Q
0 1 2
wrapped-around configuration
Q
0 1 2
f
Queues
52
53
Queues
54
Queues
55
Queues
56
qback
qfro nt
qback
qback
A
C
qfro nt
I n s e r t e l e m e n t s A ,B , C R e m o v e e l e m e n t A
qfro nt
R e m o ve e le m e n t B
D
qback
q fro n t
q b a c k q fro n t
I n s e r t e le m e n t D , I n s e r t e le m e n t E
qback
qback
qfro nt
In s e r t e le m e n t D ,
D
q fro n t C
q fro n t C
qback
I n s e r t e le m e n t D , I n s e r t e le m e n t E
A r r a y V ie w
C ir c u la r V ie w
Queues
57
Methods to Implement
i = (( i + 1) == max) ? 0 : (i + 1);
if (( i + 1) == max) i = 0; else i = i + 1;
i = ( i + 1) % max;
Queues
58
Queue Operations
We use the
modulo operator
(remainder of
division)
Algorithm size()
return (N f + r) mod N
Algorithm isEmpty()
return (f r)
Q
0 1 2
0 1 2
Q
f
Queues
59
Algorithm enqueue(o)
if size() = N 1 then
throw FullQueueException
else
Q[r] o
r (r + 1) mod N
Q
0 1 2
0 1 2
Q
f
Queues
60
Operation dequeue
throws an exception if
the queue is empty
This 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
Q
0 1 2
0 1 2
Q
f
Queues
61
Boundary Conditions
Queues
62
Implementation
The physical model: a linear array with the front
Considerations
63
Queues
64
Implementing a Queue
Efficiency of operations
Queues
65
nodes
Queues
elements
66
Queues
67
Queues
68
Queues
69
Queues
70
Queues
71
vector Implementation
Queues
72
Queues
73
Queues
74
Queues
75
The deque
76
Queues
77
Queues
78
Queues
79
Queues
80
Queues
81
deque<string>words;
string word;
Whats output?
Queues
82
Queues
83
Queues
84
Queues
85
Queues
86
87
map
start
yes
true
now
good
right
love
clear
finish
Queues
88
Queues
89
Queues
90
91
map
start
yes
true
now
good
right
love
clear
finish
Queues
92
Queues
93
1. BLOCK NUMBER
= (index + offset of first item in first block) / block size
= (5 + start.current start.first) / 4
= (5 + 2) / 4
=1
94
Queues
95
Queues
96
Queues
97
Queues
98
Priority Queue
Job # 4
S u p e r v is o r
Job # 3
C le r k
Job # 2
P r e s id e n t
99
CLASS priority_queue
Constructor
<queue>
priority_queue();
Create an empty priority queue. Type T must
implement the operator <.
CLASS priority_queue
Operations
<queue>
100
CLASS priority_queue
Operations
<queue>
101
PQ Implementation
Queues
102
Summary Slide 1
- Queue
Queues
103 10
Summary Slide 2
Queues
104 10
Summary Slide 3
Queues
105 10
Summary Slide 4
- Priority queue
Queues
106 10