Professional Documents
Culture Documents
Week 3 - Queue
Week 3 - Queue
Week 3 - Queue
Queue
Week 3
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Review
See the exercise (Week 2)
Exercise
• Number [ ] = 60, 20, 15, 88, 34, 45, 59, 51, 23, 90, 22.
• Key = 15
• Draw illustration to find “Key” in “Number”
Illustration
Key = 15 Index 0 1 2 3 4 5 6 7 8 9 10
Number 15 20 22 23 34 45 51 59 60 88 90
• Iteration 1
Variable F M L
first = 0
last = 10
middle = (0+10) / 2 = 5
Number [5] > key
Number 15 20 22 23 34 45 51 59 60 88 90
• Iteration 2
Variable F M L
first = 0
last = 4
middle = (0+4) / 2 = 2
Number [2] > key
Number 15 20 22 23 34 45 51 59 60 88 90
• Iteration 3
Variable F L
first = 0
M
last = 1
middle = (0+1) / 2 = 0
Number [0] == key
Index 0 1 2 3 4
Array
Front / Rear
Index 0 1 2 3 4
Array 15 20
Front Rear
Queue using Array: linear ordering
• EnQueue (25)
Index 0 1 2 3 4
Array 15 20 25
Front Rear
• EnQueue (30)
Index 0 1 2 3 4
Array 15 20 25 30
Front Rear
Queue using Array: linear ordering
• It can be seen on the previous slide, if a client (number)
enters the queue (EnQueue), Rear will move forward
indicating the last client to be served.
• Otherwise, if the client (number) has been served and
leaves the queue (DeQueue), Front will move forward
indicating the next client to be served.
Index 0 1 2 3 4
Array 20 25 30
Front Rear
• DeQueue ()
Index 0 1 2 3 4
Array 25 30
Front Rear
The Problem of Array: linear ordering
• EnQueue (35)
Index 0 1 2 3 4
Array 25 30 35
Front Rear
• EnQueue (40)
Index 0 1 2 3 4
Array 25 30 35
Front Rear
The last command cannot run because Array is considered to be Full (Rear is already in the last
index), even though indexes 0 and 1 are empty. What is the solution?
Queue: Move forward
Index 0 1 2 3 4
Array 40 25 30 35
Rear Front
• EnQueue (45)
Index 0 1 2 3 4
Array 40 45 25 30 35
Rear Front
Note : change the dataType with the actual data type of the
data that you want to store in the Queue.
Property & some Methods in Queue
• Property: Count ( ), Gets the number of actual elements
contained in the Queue
• Methods (for example, they are used on object called myQ):
– myQ.Enqueue (data); //add data in queue
– myQ.Dequeue(); //remove first data in queue
– myQ.Peek(); //see the 1st data without remove it
– myQ.Clear(); //remove all data without getting them
Queue
CS -1 CS - 2
Minute - 2
Customer-1 Customer-2
(1 minutes) (3 minutes)
Queue
Exercise (Example)
Minute - 3 CS -1 CS - 2
Customer-3 Customer-2
Note: Customer-1 out
(2 minute) (2 minutes)
Queue
CS -1 CS - 2
Minute - 4
Customer-3 Customer-2
(1 minutes) (1 minutes)
Queue Customer-4
Exercise (Example)
CS -1 CS - 2
Minute - 5
Customer-4 Customer-5
Note: Customer-2 and 3 out
(3 minute) (2 minute)
Queue
And so on………..