0% found this document useful (0 votes)
3 views

05-06-Queue

This document provides an overview of queues as an abstract data structure, detailing their operations such as enqueue (insertion) and dequeue (removal), and their implementation using arrays and linked lists. It emphasizes the FIFO (first-in, first-out) principle and includes examples of how to manage a queue in a practical scenario, such as processing customer requests in a bank. The document also outlines algorithms for inserting and deleting elements from a queue implemented through an array.

Uploaded by

mmnw.coet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

05-06-Queue

This document provides an overview of queues as an abstract data structure, detailing their operations such as enqueue (insertion) and dequeue (removal), and their implementation using arrays and linked lists. It emphasizes the FIFO (first-in, first-out) principle and includes examples of how to manage a queue in a practical scenario, such as processing customer requests in a bank. The document also outlines algorithms for inserting and deleting elements from a queue implemented through an array.

Uploaded by

mmnw.coet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 113

Data Structures

Lecture #05 - #06: Queues

IT & Computer Department


Spring Term 2024 – Semester-7

Presented by: Munir Khalifa.


Today’s Outline

 What is a Queue?
 Queue Operation
 Queue implementation
Data Structures
What is a Queue?

4
Basic Idea

Queue is an abstract data structure, somewhat similar to


Stacks. Unlike stacks, a queue is open at both ends. One
end is always used to insert data (enqueue) and the other is
used to remove data (dequeue).

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.

FRONT REAR REAR


F

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.

FRONT FRONT REAR

B A E C D F
Queue

The queue can be implemented in two ways:


 Using arrays (Static implementation)
 Using pointer (Dynamic implementation)
Implementation of Queue (Array)
use Array with front and back pointers as implementation of
queue
Queue

arr 0 1 3 6 7 8 9
2 4 5

A B C D E F G

back
front
Implementing a Queue

Using an array to implement a queue is significantly


harder than using an array to implement a stack.
Why?
 Unlike a stack, where we add and remove at the same
end, in a queue we add to one end and remove from the
other.
Implementation of Queue (Linked List)
Can use Linked List as underlying implementation of Queues
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.)

 Write an algorithm to insert values in a queue implemented


through array.
 Algorithm to insert values in a queue:
1. If the queue is empty:

a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position REAR in the array.


Implementing a Queue Using an Array (Contd.)

Let us now insert request numbers in If the queue is empty:

the following
Request queue.
number generated 3 Set FRONT = 0.

Increment REAR by 1.

Store the element at index position REAR


in the array.

FRONT = –1
10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 3 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


REAR in the array.

FRONT = –1  10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 3 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR in the array.

FRONT = –1  10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 3 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 0 REAR in the array.

 10
REAR = –1
0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 3 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 0 REAR in the array.

3
 10
0 1 2 3 4

Insertion complete
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 5 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REARREAR
=0 =1 REAR in the array.

310

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 5 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 1 REAR in the array.

310 5

0 1 2 3 4

Insertion complete
Implementing a Queue Using an Array (Contd.)
1. If the queue is empty:

Request number generated 7 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 1 REAR in the array.

310 5

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 7 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 1 REAR in the array.

310 5

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 7 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = REAR
1 =2 REAR in the array.

310 5

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 7 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 2 REAR in the array.

310 5 7

0 1 2 3 4

Insertion complete
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 10 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 2 REAR in the array.

310 5 7

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 10 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 2 REAR in the array.

310 5 7

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 10 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = REAR
2 =3 REAR in the array.

310 5 7

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 10 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 3 REAR in the array.

310 5 7 10

0 1 2 3 4

Insertion complete
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 15 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 3 REAR in the array.

310 5 7 10

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 15 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 3 REAR in the array.

310 5 7 10

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 15 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR =REAR
3 =4 REAR in the array.

310 5 7 10

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty:

Request number generated 15 a. Set FRONT = 0.

2. Increment REAR by 1.

3. Store the element at index position


FRONT = 0 REAR = 4 REAR in the array.

310 5 7 10 15

0 1 2 3 4

Insertion complete
Implementing a Queue Using an Array (Contd.)

The requests stored in the queue are served on a


first-come-first-served basis.
As the requests are being served, the corresponding
request numbers needs to be deleted from the queue.
Implementing a Queue Using an Array (Contd.)

Write an algorithm to delete an element from a queue


implemented through array.
Algorithm to delete an element from a queue:
1. Retrieve the element at index FRONT.

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

 One request 1. Retrieve the element at index


FRONT.
processed
2. Increment FRONT by 1.

FRONT = 0 REAR = 4

310 5 7 10 15

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

1. Retrieve the element at index


FRONT.

2. Increment FRONT by 1.

FRONT = 0FRONT = 1 REAR = 4

 10 5 7 10 15

0 1 2 3 4

Delete operation complete


Implementing a Queue Using an Array (Contd.)

One request processed 1. Retrieve the element at index


FRONT.

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

One request processed 1. Retrieve the element at index


FRONT.

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

1. Retrieve the element at index


FRONT.

2. Increment FRONT by 1.

FRONT = FRONT
1 =2 REAR = 4

10 7 10 15

0 1 2 3 4

Delete operation complete


Implementing a Queue Using an Array (Contd.)

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

To maintain FRONT at zero index position, every delete


operation would require you to shift all the succeeding
elements in the array one position left.
Let us implement a delete operation on the following queue.

FRONT = 0 REAR = 3

510 5 7 10

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

To maintain FRONT at zero index position, every delete


operation would require you to shift all the succeeding
elements in the array one position left.
Let us implement a delete operation on the following queue.

FRONT = 0 REAR = 3

510 7 7 10

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

To maintain FRONT at zero index position, every delete


operation would require you to shift all the succeeding
elements in the array one position left.
Let us implement a delete operation on the following queue.

FRONT = 0 REAR = 3

510 7 10 10

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

To maintain FRONT at zero index position, every delete


operation would require you to shift all the succeeding
elements in the array to one position left.
Let us implement a delete operation on the following queue.

FRONT = 0 REAR = 2REAR = 3

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

An effective way to solve this problem is to implement the


queue in the form of a circular array.
In this approach, if REAR is at the last index position and if
there is space in the beginning of an array, then you can set
the value of REAR to zero and start inserting elements from
the beginning.

REAR = 0 FRONT = 3 REAR = 4

Insert 5
510 10 15

0 1 2 3 4
Implementing a Queue Using an Array (Contd.)

The cells in the array are treated as if they are arranged in a


ring.

[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

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 15 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 3

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 15 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 3

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 15 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REARREAR
=3 =4

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 15 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 4

2. If REAR is at the last index position:

10 20 23 10 15 a. Set REAR = 0
b. Go to step 4
0 1 2 3 4
3. Increment REAR by 1

Insertion complete 4. Queue[REAR] = element


Implementing a Queue Using an Array (Contd.)

1. If the queue is empty (If FRONT= –1):

Request number generated 17 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 4

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 17 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 4

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 17 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
FRONT = 1 REAR = 4

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 17 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
REAR = 0 FRONT = 1 REAR = 4

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 17 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
REAR = 0 FRONT = 1

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):

Request number generated 17 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
REAR = 0 FRONT = 1

2. If REAR is at the last index position:

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

Insertion complete 4. Queue[REAR] = element


Implementing a Queue Using an Array (Contd.)

1. If the queue is empty (If FRONT= –1):

Request number generated 25 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
REAR = 0 FRONT = 1

2. If REAR is at the last index position:

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

1. If the queue is empty (If FRONT= –1):


Request number generated 25
a. Set FRONT = 0
b. Set REAR = 0
REAR = 0 FRONT = 1
c. Go to step 4

2. If REAR is at the last index position:


17
10 20 23 10 15
a. Set REAR = 0
0 1 2 3 4 b. Go to step 4

3. Increment REAR by 1

4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty (If FRONT= –1):


Request number generated 25
a. Set FRONT = 0
b. Set REAR = 0
REAR = 0 FRONT = 1
c. Go to step 4

2. If REAR is at the last index position:


17
10 20 23 10 15
a. Set REAR = 0
0 1 2 3 4 b. Go to step 4

3. Increment REAR by 1

4. Queue[REAR] = element
Implementing a Queue Using an Array (Contd.)

1. If the queue is empty (If FRONT= –1):

Request number generated 25 a. Set FRONT = 0


b. Set REAR = 0
c. Go to step 4
REAR = 0 FRONT = 1

2. If REAR is at the last index position:

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

queue full condition.


Implementing a Queue Using an Array (Contd.)

The conditions for queue full are as follows:

If FRONT = 0 and REAR If FRONT = REAR + 1


is at the last index position
OR
FRONT = 0 REAR = 4
REAR = 2 FRONT = 3

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

Modified algorithm for inserting an 1. If the queue is full:

element in a circular queue. a. Display “Queue overflow”


b. Exit

2. If queue is empty (If FRONT = –1):

a. Set FRONT = 0
b. Set REAR = 0
c. Go to Step 5

3. If REAR is at the last index position:

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

Algorithm to delete an element 1. If there is only one element in the


queue:
from a circular queue.
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.)

One request processed 1. If there is only one element in the


queue:

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

One request processed


1. If there is only one element in the
queue:

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
0 1 2 3 4 b. Exit

3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

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

One request processed 1. If there is only one element in the


queue:

a. Set FRONT = –1
b. Set REAR = –1
c. Exit
FRONT = 0
REAR = 0
2. If FRONT is at the last index
position:

FRONT = –1 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.)

One request processed 1. If there is only one element in the


queue:

a. Set FRONT = –1
b. Set REAR = –1
c. Exit
REAR = 0
2. If FRONT is at the last index
position:

FRONT = –1 10 a. Set FRONT = 0


b. Exit
REAR = –1 0 1 2 3 4
3. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)

One request processed 1. If there is only one element in the


queue:

a. Set FRONT = –1
b. Set REAR = –1
c. Exit

2. If FRONT is at the last index


position:

FRONT = –1 10 a. Set FRONT = 0


b. Exit
REAR = –1 0 1 2 3 4
3. Increment FRONT by 1

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

2. If FRONT is at the last index


position:

FRONT = –1 10 a. Set FRONT = 0


b. Exit
REAR = –1 0 1 2 3 4
3. Increment FRONT by 1

Therefore, before implementing a


delete operation, you first need to
check whether the queue is
empty or not.
Implementing a Queue Using an Array (Contd.)

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

2. If there is only one element in the


queue: // If FRONT = REAR

a. Set FRONT = –1
b. Set REAR = –1
c. Exit

3. If FRONT is at the last index


position:

a. Set FRONT = 0
b. Exit

4. Increment FRONT by 1
Implementing a Queue Using an Array (Contd.)

What is the advantage of implementing a queue in the form


of a circular array, instead of a linear array structure?

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

To keep track of the rear and front positions, you need to


declare two variables/pointers, REAR and FRONT, that will
always point to the rear and front end of the queue
respectively.
If the queue is empty, REAR and FRONT point to NULL.

FRONT REAR

310 5 7 15
Inserting an Element in a Linked Queue

Write an algorithm to implement insert operation in a linked


queue.
Inserting an Element in a Linked Queue (Contd.)

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.

3. Make the next field of the new node point to


REAR = NULL NULL.

FRONT = NULL
4. If the queue is empty, execute the following
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit

5. Make the next field of REAR point to the new


node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

Request number generated 3 1. Allocate memory for the new node.

2. Assign value to the data field of the new


node.

REAR = NULL 3. Make the next field of the new node point to
FRONT = NULL NULL.

4. If the queue is empty, execute the following


steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit

5. Make the next field of REAR point to the new


node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


REAR = NULL NULL.

FRONT = NULL
4. If the queue is empty, execute the following
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


REAR = NULL NULL.

FRONT = NULL
4. If the queue is empty, execute the following
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


REAR = NULL NULL.

FRONT = NULL
4. If the queue is empty, execute the following
steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


REAR = NULL NULL.

FRONT = NULL
4. If the queue is empty, execute the following
FRONT steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


REAR = NULL NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 3
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

Insert operation complete


6. Make REAR point to the new node.
Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310 510
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310 510
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310 510
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310 510
5. Make the next field of REAR point to the new
node.

6. Make REAR point to the new node.


Inserting an Element in a Linked Queue (Contd.)

1. Allocate memory for the new node.


Request number generated 5
2. Assign value to the data field of the new
node.

3. Make the next field of the new node point to


NULL.

4. If the queue is empty, execute the following


FRONT REAR REAR steps:

a. Make FRONT point to the new node


b. Make REAR point to the new node
c. Exit
310 510
5. Make the next field of REAR point to the new
node.

Insert operation complete


6. Make REAR point to the new node.
Deleting an Element from a Linked Queue

Write an algorithm to implement the delete operation on a


linked queue.
Deleting an Element from a Linked Queue (Contd.)

Algorithm
One to implement delete
request processed 1. If the queue is empty: // FRONT = NULL

operation on a linked queue. a. Display “Queue empty”


b. Exit

2. Mark the node marked FRONT as current

3. Make FRONT point to the next node in its


FRONT REAR sequence

4. Release the memory for the node marked


310 5 7 10 as current
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

2. Mark the node marked FRONT as current

3. Make FRONT point to the next node in its


FRONT REAR sequence

4. Release the memory for the node marked


310 5 7 10 as current
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

2. Mark the node marked FRONT as current

3. Make FRONT point to the next node in its


FRONT REAR sequence

4. Release the memory for the node marked


310 5 7 10 as current

current
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

2. Mark the node marked FRONT as current

3. Make FRONT point to the next node in its


FRONT FRONT REAR sequence

4. Release the memory for the node marked


310 5 7 10 as current

current
Deleting an Element from a Linked Queue (Contd.)

One request processed 1. If the queue is empty: // FRONT = NULL

a. Display “Queue empty”


b. Exit

2. Mark the node marked FRONT as current

3. Make FRONT point to the next node in its


FRONT REAR sequence

4. Release the memory for the node marked


3 5 7 10 as current

current
Delete operation complete

Memory released
Applications of Queues

Queues offer a lot of practical applications, such as:


Printer Spooling
CPU Scheduling
Mail Service
Keyboard Buffering
Elevator

You might also like