05-06-Queue
05-06-Queue
What is a Queue?
Queue Operation
Queue implementation
Data Structures
What is a Queue?
4
Basic Idea
5
What is a Queue?
Queue is a list of elements in which an element is inserted
at one end and deleted from the other end of the queue.
Elements are inserted at the rear end and deleted from the
front end.
FRONT REAR
B A E C D
What is a Queue? Cont..
• A data structure of ordered items such that items can be inserted
only at one end and removed at the other end.
• Queues implement the FIFO (first-in first-out) policy. E.g., a
printer/job queue!
• Two basic operations of queues:
dequeue: remove an item/element from front
enqueue: add an item/element at the back
dequeue enqueue
Operations on Queues
Insert (enqueue): 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.
B A E C D F
Operations on Queues (Contd.)
Delete (dequeue): 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
Queue
arr 0 1 3 6 7 8 9
2 4 5
A B C D E F G
back
front
Implementing a Queue
lst
LinkedList
head tail
a1 a2 a3 a4
Implementing a Queue Using an Array
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 need to be
queued into the system and processed based on 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.)
How
To cantrack
insert
keep ayou solve
request this
rearproblem?
of thenumber, andyou need
front to perform
positions, you the
need to
following
declare two
You can steps:
integer
solve thisvariables,
problem byREAR and FRONT.
implementing a queue.
Increment the value of REAR by 1.
If
Letthe
usqueue is empty,
implement REAR
a queue andan
using FRONT are set
array that to –1.
stores these
Insert the element at index position REAR in the array.
request numbers in the order of their arrival.
FRONT = –1
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = 0.
2. Increment REAR by 1.
the following
Request queue.
number generated 3 Set FRONT = 0.
Increment REAR by 1.
FRONT = –1
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
FRONT = –1 10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
FRONT = –1 10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
3
10
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
1. If the queue is empty:
2. Increment REAR by 1.
310 5
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment REAR by 1.
310 5 7 10 15
0 1 2 3 4
Insertion complete
Implementing a Queue Using an Array (Contd.)
2. Increment FRONT by 1.
Implementing a Queue Using an Array (Contd.)
OneLet
request processed
us see how requests are 1. Retrieve the element at index
FRONT.
deleted from the queue once they
get processed. 2. Increment FRONT by 1.
FRONT = 0 REAR = 4
3 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 4
310 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment FRONT by 1.
10 5 7 10 15
0 1 2 3 4
2. Increment FRONT by 1.
FRONT = 1 REAR = 4
10 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment FRONT by 1.
FRONT = 1 REAR = 4
10 5 7 10 15
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
2. Increment FRONT by 1.
FRONT = FRONT
1 =2 REAR = 4
10 7 10 15
0 1 2 3 4
To implement
As you delete an insert or
elements delete
from the operation,
queue, theyou need
queue to
moves
increment
down the values of REAR or FRONT by one,
the array.
respectively.
The disadvantage of this approach is that the storage space
However,
in these values
the beginning are never
is discarded and decremented.
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.
Implementing a Queue Using an Array (Contd.)
How
To can youFRONT
maintain solve this problem?
at zero index position, every delete
operation
One way would require
to solve you to shift
this problem all the
is to keep succeeding
FRONT always at the
elements in the
zero index array one position left.
position.
Let Refer to the following
us implement queue.
a delete operation on the following queue.
FRONT = 0 REAR = 3
3
10 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 3
510 5 7 10
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
FRONT = 0 REAR = 3
510 7 7 10
0 1 2 3 4
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.)
Advantage of this
Disadvantage of this
approach:
approach:
Every
It enables
delete
youoperation
to utilize requires
all the empty
you topositions
shift all the
in an
succeeding
array.
Therefore,inunlike
elements the queue
the previous
one position
case,left.
there is no wastage of
space.
If the list is lengthy, this can be very time consuming.
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.)
Let
Write
usan
now algorithm
implement
to insert
a few 1. If the queue is empty (If FRONT= –1):
Requestinsert
values
numberoperations
ingenerated
a queueon implemented
the
15 following a. Set FRONT = 0
circular
as a circular
queue:array. b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 3
10 20 23 10 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
17
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
17
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
3. Increment REAR by 1
4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
17
10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1
Before
REAR
Inserting
cannot
implementing
an
beelement
incremented
an insert
in a full
operation,
because
queue leads
the
you
queue
should
to queue
is always
full. Queue[REAR]
check for =the
overflow.
4. element
310 5 7 10 15 17
10 20 23 10 15
0 1 2 3 4 0 1 2 3 4
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = 0
b. Set REAR = 0
c. Go to Step 5
a. Set REAR = 0
b. Go to step 5
4. Increment REAR by 1
5. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)
Now let
Write anus implement
algorithm a delete operation
to implement on a queue
delete operation in a queue
implemented as in the form ofarray.
a circular a circular array.
To delete an element, you need to increment the value of
FRONT by one. This is same as that of a linear queue.
However, if the element to be deleted is present at the last
index position, then the value FRONT is reset to zero.
If there is only one element present in the queue, then the
value of FRONT and REAR is set to –1.
Implementing a Queue Using an Array (Contd.)
17
10 20 15 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
REAR = 0 FRONT = 4
2. If FRONT is at the last index
position:
17
10 20 15 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
REAR = 0 FRONT = 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
REAR = 0 FRONT = 4
2. If FRONT is at the last index
position:
17
10 20 15 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0 REAR = 0 FRONT = 4
2. If FRONT is at the last index
position:
17 20 15
10 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0 REAR = 0
2. If FRONT is at the last index
position:
17
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Deletion complete
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0 REAR = 0
2. If FRONT is at the last index
position:
17
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0 REAR = 0
2. If FRONT is at the last index
position:
17
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0 REAR = 0
2. If FRONT is at the last index
position:
17
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0
FRONT = 0 REAR = 0
2. If FRONT is at the last index
position:
17
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Deletion complete
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0
REAR = 0
2. If FRONT is at the last index
position:
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0
REAR = 0
2. If FRONT is at the last index
position:
10 20 a. Set FRONT = 0
b. Exit
0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0
REAR = 0
2. If FRONT is at the last index
position:
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
REAR = 0
2. If FRONT is at the last index
position:
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
Deletion complete
Implementing a Queue Using an Array (Contd.)
At this
The stage,
queue if you
is now try to
empty. 1. If there is only one element in the
queue:
implement a delete operation, it
will result in queue underflow. a. Set FRONT = –1
b. Set REAR = –1
c. Exit
The
Modified
condition
algorithm
for queue
for deleting
empty an
is: 1. If the queue is empty: // If
// FRONT
element from a circular
FRONT = –1 queue: = –1
a. Display “Queue underflow”
b. Exit
a. Set FRONT = –1
b. Set REAR = –1
c. Exit
a. Set FRONT = 0
b. Exit
4. Increment FRONT by 1
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
FRONT REAR
310 5 7 15
Inserting an Element in a Linked Queue
Algorithminitially,
Suppose to insertthe
anqueue 1. Allocate memory for the new node.
Request number 3
element
is empty.in a linked queue. 2. Assign value to the data field of the new
generated node.
FRONT = NULL
4. If the queue is empty, execute the following
steps:
REAR = NULL 3. Make the next field of the new node point to
FRONT = NULL NULL.
FRONT = NULL
4. If the queue is empty, execute the following
steps:
FRONT = NULL
4. If the queue is empty, execute the following
steps:
FRONT = NULL
4. If the queue is empty, execute the following
steps:
FRONT = NULL
4. If the queue is empty, execute the following
FRONT steps:
Algorithm
One to implement delete
request processed 1. If the queue is empty: // FRONT = NULL
current
Deleting an Element from a Linked Queue (Contd.)
current
Deleting an Element from a Linked Queue (Contd.)
current
Delete operation complete
Memory released
Applications of Queues