W9L1 Queues
W9L1 Queues
Week 9
Lectures 1-2
In this session, you will learn to:
Objectives
Overview of Queue
Applications of Queues
Insertion and deletion in a Linear
queue
Implementation of Circular Queue
Dequeue
Priority Queue
Consider a situation where you have to
create an application with the following set
of requirements:
4
Ver. 1.0 Session 11
Data Structures and Algorithms
Defining Queues
How can you solve this problem?
5
Ver. 1.0 Session 11
Defining Queues (Contd.)
Elements are inserted at the rear end and deleted
from the front end.
Queue is a list of elements in which an element is
inserted at one end and deleted from the other end
of the queue.
FRONT REAR
B A E C D
Just a minute
Answer:
FIFO
Various operations implemented on a queue are:
Identifying Various Operations on Queues
Insert (Enqueue)
Delete (Dequeue)
FRONT REAR
B A E C D
Insert: It refers to the addition of an item in the
queue.
Suppose you want to add an item F in the
following queue.
Since the items are inserted at the rear end,
therefore, F is inserted after D.
Now F becomes the rear end.
FRONT REAR REAR
F
B A E C D F
Delete: It refers to the deletion of an item from the
queue.
Since the items are deleted from the front end,
therefore, item B is removed from the queue.
Now A becomes the front end of the queue.
B A E C D F
Queues are the data structures in which data can
be added at one end called ______ and deleted
from the other end called _________.
Answer:
rear, front
Problem Statement:
Consider a scenario of a bank. When the customer visits
the counter, a request entry is made and the customer
is given a request number. After receiving request
numbers, a customer has to wait for some time. The
customer requests needs to be queued into the system
and processed on the basis of their arrival. You need to
implement an appropriate data storage mechanism to
store these requests in the system.
Implementing a Queue Using an Array (Contd.)
To keep track of the rear and front positions, you need to
declare two integer variables, REAR and FRONT.
If the queue is empty, REAR and FRONT are set to –1.
How can you solve this problem?
You can solve this problem by implementing a queue.
Let us implement a queue using an array that stores these
request numbers in the order of their arrival.
FRONT = –1
REAR = –1
0 1 2 3 4
FRONT = –1
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
FRONT = –1
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
FRONT = –1
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
310
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
310 5
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5 7
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
310 5 7
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5 7
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5 7 10
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
310 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5 7 10 15
0 1 2 3 4
Insertion complete
Algorithm to insert data in a Queue
FRONT = -1, REAR = -1, TO REPRESENT QUEUE IS EMPTY
Algorithm INSERT(QUEUE[N],FRONT,REAR,ITEM)
{
//QUEUE is an array of size N ,ITEM is element to be inserted.
1. if (REAR == N-1)
1.1 Print “OVERFLOW”
else
1.1 if (FRONT == -1)
1.1.1 FRONT = 0
1.2 REAR = REAR+1
1.3 QUEUE[REAR] = ITEM
}
The requests stored in the queue are served on
a first-come-first-served basis.
FRONT = 0 REAR = 4
3 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
310 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
10 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1.If( FRONT == – 1 )
1.1 print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[FRONT]
2.2 FRONT = FRONT +1
FRNT = 0 FRONT = 1 REAR = 4
10 5 7 10 15
0 1 2 3 4
1.If( FRONT == – 1 )
1.1 print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[FRONT]
2.2 FRONT = FRONT +1
FRONT = FRONT
1 =2 REAR = 4
10 7 10 15
0 1 2 3 4
Algorithm DELETE(QUEUE[N],ITEM,FRONT,REAR)
{
1. if (( FRONT == – 1 ) || (FRONT == REAR+1))
1.1 Print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[FRONT]
2.2 FRONT = FRONT +1
}
38
Ver. 1.0 Session 11
Data Structures and Algorithms
Applications of Queues
39
Ver. 1.0 Session 11
Data Structures and Algorithms
PRINTER SPOOLING
• A printer may receive multiple print requests in a
short span of time.
• The rate at which these requests are received is
much faster than the rate at which they are
processed.
• Therefore, a temporary storage mechanism is
required to store these requests in the order of
their arrival.
• A queue is the best choice in this case, which
stores the print requests in such a manner so that
they are processed on a first-come-first-served
basis.
40
Ver. 1.0 Session 11
Data Structures and Algorithms
CPU Scheduling
A CPU can process one request at a time.
The rate at which the CPU receives requests is usually much
greater than the rate at which the CPU processes the
requests.
Therefore, the requests are temporarily stored in a queue in
the order of their arrival.
Whenever CPU becomes free, it obtains the requests from
the queue.
Once a request is processed, its reference is deleted from the
queue.
The CPU then obtains the next request in sequence and the
process continues.
In a time sharing system, CPU is allocated to each request
for a fixed time period.
41
Ver. 1.0 Session 11
Data Structures and Algorithms
CPU Scheduling (Contd.)
42
Ver. 1.0 Session 11
Data Structures and Algorithms
Mail Service
43
Ver. 1.0 Session 11
Data Structures
Keyboard and Algorithms
Buffering
As you delete elements from the queue, the queue moves down the array.
The disadvantage of this approach is that the storage space in the beginning is
discarded and never used again.
Consider the following queue.
FRONT = 3 REAR = 4
10 10 15
0 1 2 3 4
REAR is at the last index position.
Therefore, you cannot insert elements in this queue, even though there is space for them.
This means that all the initial vacant positions go waste.
46
Ver. 1.0 Session 11
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)
310 5 7 10
0 1 2 3 4
47
Ver. 1.0 Session 11
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 3
510 5 7 10
0 1 2 3 4
48
Ver. 1.0 Session 11
Data Structures and Algorithms
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 3
510 7 7 10
0 1 2 3 4
49
Ver. 1.0 Session 11
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 3
510 7 10 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
510 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 2
510 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
Insert 5
510 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
[0]
[4] [1]
REAR = 3 10 7 FRONT = 2
[3] [2]
Implementing a Queue Using an Array (Contd.)
FRONT = 1 REARREAR
=3 =4 1. IF(FRONT==-1 )
1.1 FRONT = 0
1.2 REAR = 0
10 20 23 10 1.3 GO TO STEP 4
2. IF ( REAR == N-1 )
0 1 2 3 4 2.1 REAR = 0
2.2 GO TO STEP 4
3. REAR = REAR + 1
4. QUEUE[REAR] = ITEM
Implementing a Queue Using an Array (Contd.)
310 5 7 10 15 17
10 20 23 10 15
0 1 2 3 4 0 1 2 3 4
Algorithm toand
Data Structures insertAlgorithms
data in a Circular Queue
FRONT = -1, REAR = -1, TO REPRESENT QUEUE IS EMPTY
Algorithm INSERT(QUEUE[N],FRONT,REAR,ITEM)
{
//QUEUE is an array of size N ,ITEM is element to be inserted.
1. if ((FRONT == REAR+1) || ((FRONT == 0) && (REAR == N-1)))
1.1 DISPLAY “QUEUE OVERFLOW”
1.2 exit
2. else
2.1 if(FRONT == -1)
2.1.1 FRONT = 0
2.1.2 REAR = 0
2.1 else if ( REAR == N-1 )
2.1.1 REAR = 0
2.1 else
2.1.1 REAR = REAR + 1
2.2 QUEUE[REAR] = ITEM
} Ver. 1.0 62
Session 11
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
REAR = 1 FRONT = 4 1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
17 20 15 2.2 EXIT
10
3. FRONT = FRONT + 1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
FRONT = 0 REAR = 1 1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
17 20 2.2 EXIT
10
3. FRONT = FRONT + 1
0 1 2 3 4
Deletion complete
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
FRONT = 0 REAR = 1 1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
17 20 2.2 EXIT
10
3. FRONT = FRONT + 1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
FRONT = 1
REAR = 1 1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
20 2.2 EXIT
10
3. FRONT = FRONT + 1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
FRONT = 1
REAR = 1 1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
20 2.2 EXIT
10
3. FRONT = FRONT + 1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR =-1
FRONT = 1
REAR = 1 1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
20 2.2 EXIT
FRONT = –1 10
3. FRONT = FRONT + 1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
2.2 EXIT
FRONT = –1 10
3. FRONT = FRONT + 1
REAR = –1 0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
1. IF(FRONT== REAR)
1.1 FRONT = -1
1.2 REAR = -1
1.3 EXIT
2. IF ( FRONT == N-1 )
2.1 FRONT = 0
2.2 EXIT
FRONT = –1 10
3. FRONT = FRONT + 1
REAR = –1 0 1 2 3 4
Deletion complete
Implementing a Queue Using an Array (Contd.)
Answer:
If you implement a queue in the form of a linear array, you can
add elements only in the successive index positions. However,
when you reach the end of the queue, you cannot start
inserting elements from the beginning, even if there is space
for them at the beginning. You can overcome this
disadvantage by implementing a queue in the form of a
circular array. In this case, you can keep inserting elements till
all the index positions are filled. Hence, it solves the problem
of unutilized space.
Implementing a Queue Using a Linked List
77
Ver. 1.0 Session 11
DEQUEUE
•Double-ended Queue (dequeue), often abbreviated to
Deque.
1. if (REAR == N-1)
1.1 PRINT “OVERFLOW”
2. else
2.1 IF(FRONT==-1)
2.1.1 FRONT = 0
2.2. REAR = REAR+1
2.3 QUEUE[REAR] = ITEM
}
Algorithm to Insert / enqueue from front
FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS EMPTY
Algorithm INSERTFRONT(QUEUE[N],FRONT,REAR,ITEM)
{
//QUEUE is an array of size N ,ITEM is element to be inserted.
1. if( FRONT == 0)
1.1 Print “CAN NOT INSERT”
2. else
2.1 if (FRONT == -1)
2.1.1 FRONT = 0
2.1.2 REAR = 0
2.1 else
2.1.1 FRONT = FRONT-1
2.3 QUEUE[FRONT] = ITEM
}
Algorithm to Delete / dequeue from front
FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS EMPTY
Algorithm DELETE(QUEUE[N],ITEM,FRONT,REAR)
{
1. if (( FRONT == – 1 ) || (FRONT == REAR+1))
1.1 Print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[FRONT]
2.2 FRONT = FRONT +1
}
Data Structures
Algorithm and Algorithms
to Delete / dequeue from rear
FRONT = -1, REAR = -1, TO REPRESENT DEQUEUE IS EMPTY
Algorithm DELETE(QUEUE[N],ITEM,FRONT,REAR)
{
1. if (( FRONT == – 1 ) || (FRONT == REAR+1))
1.1 Print “QUEUE EMPTY”
2. else
2.1 ITEM = QUEUE[REAR]
2.2 REAR = REAR - 1
}
84
Ver. 1.0 Session 11
APPLICATION OF DEQUEUE
• Job scheduling algorithm.
86
Ver. 1.0 Session 11
Data Structures and Algorithms
APPLICATION OF PRIORITY QUEUE
87
Ver. 1.0 Session 11
Data Structures and Algorithms
ALGORITHM FOR PRIORITY QUEUES
int pqueue[10];
int front, rear;
void main()
{
int n,
front = rear = -1;
if (rear >= MAX - 1)
{ printf("\nQUEUE OVERFLOW"); return;
}
if ((front == -1) && (rear == -1))
{
front++;
rear++;
pqueue[rear] = data;
return;
}