Week 3 - Queue

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Data Structure

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

because Number [middle] > key:


last = middle - 1 = 4
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 2
Variable F M L
first = 0
last = 4
middle = (0+4) / 2 = 2
Number [2] > key

because Number [middle] > key:


last = middle - 1 = 1
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 3
Variable F L
first = 0
M
last = 1
middle = (0+1) / 2 = 0
Number [0] == key

because Number [middle] == key:


STOP ITERATION
Let's start getting into
Queue material
Introduction
▪ In our everyday life, it is common to have a system where
some servers must serve several number of clients
▪ And it is very common as well that the number of clients
are bigger than the number of servers. In this situation the
clients must queue to get the service from the server.
▪ Server will serve client with any of these two common
approaches:
▪ First Come - First Served or
▪ Priority-based processing
Introduction
▪ First in, first out (FIFO) system is commonly used at the grocery
store, the bank, buy tickets, etc.
▪ All clients will usually stand in a line waiting for the service.
▪ The person in front of you will be served before you, while the
person behind you will be served after.
Introduction
▪ In Priority-based system, client with higher priority will be
served first than client with lesser priority.
▪ This system is commonly used in a hospital emergency
room.
▪ People with a potentially fatal wound would have higher
priority to be served than people with a less threatening
wound, regardless of who arrived first.
Introduction
▪ For example you were asked to create a computer program
to implement the First In First Out (FIFO) queue.
▪ What is the data type that can be used to implement the
queue?

▪ We can use: Array, ArrayList, List, or other data structure


like LinkedList (not yet discussed)
Queue
▪ There are two main processes in the queue.
▪ Add the client to the queue (EnQueue)
▪ Serve the front most client in the queue and remove it
from the queue (DeQueue)

Let’s see the illustration (next slide)


Queue using Array: linear ordering
• For example: We have an array with a capacity of 5

Index 0 1 2 3 4
Array
Front / Rear

• Front is a variable that indicates the position where a client must


be served first.
• Rear is a variable that indicates the position of the last client to
be served.
Queue using Array: linear ordering
There are several commands as follows:
• EnQueue (15)
Index 0 1 2 3 4
Array 15
Front, Rear
• EnQueue (20)

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.

See the illustration on the next slide


Queue using Array: linear ordering
• DeQueue ()

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

▪ To remove the previous problem, what do you think if we


move the elements forward when one item is removed
from the array?
▪ Is there any problem with this approach?
Better Approach : Circular Queue
Consider this situation:
• The customer service in a bank asks the person that needs
his/her service to get a card with a queue number written in it.
Only 50 cards are provided, number 1 to 50.
• In average, in one hour there will be 10 people come to the
customer service. And in average, in one hour the customer
service can serve only 9 people
• What happen to the person that will come after the 5 th hour? Can
he/she get the queue card?
Answer : The person will get the card from 1 again.
Circular Queue: Illustration
• EnQueue (40)

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

• EnQueue (50) → Queue is full


Queue in .NET
• Luckily for us, .NET already provided the Queue data
structure.
• To create a queue object:

Queue<dataType> myQ = new Queue<dataType>();

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

Note: there is no “empty” or “full” method provided in the .Net


Queue class.
Questions??
Exercise

• Bank "Love Money" has 2 Customer Service (CS) capable of


providing 2 services, namely:
1. Saving (2 minute work process)
2. Cash Withdrawal (3 minute working process)
• The customer comes to the bank every 1 minute. Every customer
who comes will get a queue number in sequence (starting from 1).
• Assume customers with odd queue numbers will save the money.
Meanwhile, customers with an even queue number will withdraw
cash.
Exercise
• Every customer who comes will be served by a CS who is not
busy, but if all CS is busy, the customer will enter a queue.
• If one of the CS has finished serving the customer, the customer
who is in the front queue will immediately be served by the CS.
• Assume the bank does not accept customers who come at the 11th
minute (the 10th minute accepts the last customer who came).
• Write/Draw simulations at the "Love Money" Bank until all
customers who come have been served.
Exercise (Example)
CS -1 CS - 2
Minute - 1
Customer-1
(2 minutes)

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

You might also like