Professional Documents
Culture Documents
Objectives
Elements
Queue is aare
listinserted
of elements
at theinrear
which
end
anand
element
deleted
is inserted
from the
at one
front end.
end and deleted from the other end of the queue.
FRONT REAR
B A E C D
Answer:
FIFO
FRONT REAR
B A E C D
B A E C D F
B A E C D F
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.
How
To cantrack
keep
insert ayou solve
request this
rearproblem?
of thenumber, andyou
front
need
positions,
to perform
you the
need to
declare
following
You two
cansteps:
integer
solve thisvariables,
problem byREAR and FRONT.
implementing a queue.
Let Increment
If the
usqueue
implementisthe value
empty, of REAR
REAR
a queue and
usingbyan
1. array that
FRONT are set to 1.
stores these
Insertnumbers
request the element at index
in the orderposition
of theirREAR in the array.
arrival.
FRONT = 1
REAR = 1
0 1 2 3 4
a. Set FRONT = 0.
2. Increment REAR by 1.
Requestinnumber
the following queue.
generated 3 a. Set FRONT = 0.
2. Increment REAR by 1.
FRONT = 1
10
REAR = 1
0 1 2 3 4
2. Increment REAR by 1.
FRONT = 1
10
REAR = 1
0 1 2 3 4
2. Increment REAR by 1.
FRONT = 1
10
REAR = 1
0 1 2 3 4
2. Increment REAR by 1.
10
REAR = 1
0 1 2 3 4
2. Increment REAR by 1.
310
0 1 2 3 4
Insertion complete
2. Increment REAR by 1.
310
0 1 2 3 4
2. Increment REAR by 1.
310
0 1 2 3 4
2. Increment REAR by 1.
310
0 1 2 3 4
2. Increment REAR by 1.
310 5
0 1 2 3 4
Insertion complete
2. Increment REAR by 1.
310 5
0 1 2 3 4
2. Increment REAR by 1.
310 5
0 1 2 3 4
2. Increment REAR by 1.
310 5
0 1 2 3 4
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
Insertion complete
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
2. Increment REAR by 1.
310 5 7
0 1 2 3 4
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
Insertion complete
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
2. Increment REAR by 1.
310 5 7 10
0 1 2 3 4
2. Increment REAR by 1.
310 5 7 10 15
0 1 2 3 4
Insertion complete
2. Increment FRONT by 1.
OneLet
request processed
us see how requests are 1. Retrieve the element at index
FRONT.
deleted from the queue once they
2. Increment FRONT by 1.
get processed.
FRONT = 0 REAR = 4
3 5 7 10 15
0 1 2 3 4
2. Increment FRONT by 1.
FRONT = 0 REAR = 4
310 5 7 10 15
0 1 2 3 4
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
2. Increment FRONT by 1.
FRONT = 1 REAR = 4
10 5 7 10 15
0 1 2 3 4
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.
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
310 5 7 10
0 1 2 3 4
FRONT = 0 REAR = 3
510 5 7 10
0 1 2 3 4
FRONT = 0 REAR = 3
510 7 7 10
0 1 2 3 4
FRONT = 0 REAR = 3
510 7 10 10
0 1 2 3 4
510 7 10
0 1 2 3 4
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
Insert 5
510 10 15
0 1 2 3 4
[0]
[4] [1]
REAR = 3 10 7 FRONT = 2
[3] [2]
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
b. Set REAR = 0
circular
as a circular
queue:array. c. Go to step 4
FRONT = 1 REAR = 3
2. If REAR is at the last index position:
a. Set REAR = 0
10 20 23 10 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
Insertion complete
a. Set REAR = 0
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
17
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
Insertion complete
a. Set REAR = 0
17
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
17
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
17
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
a. Set REAR = 0
17
10 20 23 10 15 b. Go to step 4
3. Increment REAR by 1
0 1 2 3 4
4. Queue[REAR] = element
Before
Inserting
REAR cannot
implementing
an element
be incremented
in
anainsert
full queue
operation,
because
leads
the
you
toqueue
queue
should
isoverflow.
always
full. check for the
queue full condition.
310 5 7 10 15 17
10 20 23 10 15
0 1 2 3 4 0 1 2 3 4
a. Set FRONT = 0
b. Set REAR = 0
c. Go to Step 5
a. Set REAR = 0
b. Go to step 5
2. Increment REAR by 1
3. Queue[REAR] = element
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.
a. Set FRONT = 0
b. Exit
17
10 20 15
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20 15
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20 15
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20 15
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20 15
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20
3. Increment FRONT by 1
0 1 2 3 4
Deletion complete
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
17
10 20
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
FRONT = 1
FRONT = 0 REAR = 1 2. If FRONT is at the last index
position:
a. Set FRONT = 0
b. Exit
17
10 20
3. Increment FRONT by 1
0 1 2 3 4
Deletion complete
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
FRONT = 1
REAR = 1 2. If FRONT is at the last index
position:
a. Set FRONT = 0
b. Exit
10 20
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
FRONT = 1
REAR = 1 2. If FRONT is at the last index
position:
a. Set FRONT = 0
b. Exit
10 20
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
FRONT = 1
REAR = 1 2. If FRONT is at the last index
position:
a. Set FRONT = 0
b. Exit
FRONT = 1 10 20
3. Increment FRONT by 1
0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
FRONT = 1 10
3. Increment FRONT by 1
REAR = 1 0 1 2 3 4
a. Set FRONT = 1
b. Set REAR = 1
c. Exit
a. Set FRONT = 0
b. Exit
FRONT = 1 10
3. Increment FRONT by 1
REAR = 1 0 1 2 3 4
Deletion complete
a. Set FRONT = 0
b. Exit
FRONT = 1 10
3. Increment FRONT by 1
REAR = 1 0 1 2 3 4
The
Modified
condition
algorithm
for queue
for deleting
empty an
is: 1. If the queue is empty: // If
// FRONT = 1
element from a
FRONT = circular
1 queue: 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
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.
Problem Statement:
Write a program to implement insert and delete operations on
a queue implemented in the form of a circular array.
FRONT REAR
310 5 7 15
Algorithminitially,
Suppose to insertthe
anqueue 1. Allocate memory for the new node.
Requestis
number
empty.generated
element 3
in a linked queue. 2. Assign value to the data field of the new
node.
Algorithm
One to implement delete
request processed 1. If the queue is empty: // FRONT = NULL
310 5 7 10
310 5 7 10
310 5 7 10
current
310 5 7 10
current
3 5 7 10
current
Delete operation complete
Memory released
Answer:
In a linked list, you can insert and delete elements anywhere
in the list. However, in a linked queue, insertion and deletion
takes place only from the ends. More specifically, insertion
takes place at the rear end and deletion takes place at the
front end of the queue.