DS-Unit-4-Queue
DS-Unit-4-Queue
fi
fi
on
on
on
C
C
Unit 4
Queues
l
l
ia
tia
tia
tia
nt
en
en
en
de
fid
fid
fid
Definition and Concept :
Queue is a non-primitive linear data structure, It is an ordered list of elements in which addition
on
on
on
of new element can take place from one end called rear, and deletion of existing element is done from
C
C
another end, called as front. This means that elements are removed from a queue in the same order of
that in which they were inserted into queue. As the element which inserted first in queue is deleted first
from queue so queues are also called first in first out (FIFO) lists or first come first served(FCFS).
l
l
tia
tia
tia
tia
n
en
en
en
A
Queue with its pointers
de
fid
fid
fid
Ilem 1 tem 2
tem 3 hem 4
Flo
on
on
on
REA
C
C
Frort pointer Rear palnter
Queues may be represented by a linear array with two pointer variables. Front is the pointer
l
l
containing location of front element of queue and Rear containing rear element of queue. Whenever a
ia
ia
ia
ia
nt
nt
nt
nt
new element is added to queue rear pointer is increased by one and when existing element deleted from
de
id
id
of a queue is the queue
f
f
A good real-life example of people waiting at railway station to get
on
on
on
the ticket. Each new person who will come joins at the end of queue. The person which
C
l
ia
ia
ia
ia
nt
nt
nt
Other examples
de
:
id
id
id
Operating systems often maintain a queue of processes that are ready to execute or that are
f
f
on
on
on
When placed on hold for telephone operators. For example, when you phone the toll-free
number for your bank, you may get a recording that says, "Thank you for calling A-1 Bank.
59
tia
tia
tia
tia
en
en
en
en
fi
fi
fi
on
on
on
operator. Please wait." This is a queuing
C
next available
answered by the
Your call will be
system
:
Queue
Static representation of dimensional array is
l
l
arrays. One used
ia
tia
tia
tia
queue can be obtained using
lo
of
Static representation REAR contain integer vale+
nt
en
en
of queue is N and remov
represent queue. Suppose size
de
fid
fid
represent two ends of queue. Each REAR=N
FRONT=0 will indicate that the queue is empty and
on
on
on
FRONT is incremented. Condition to 0.
Empty queue is indicated by setting FRONT and REAR value
represent that queue is full.
C
C
a an array is declared, its size cannot be modified durin
Array has few limitations. Once size of
executing program. If we store less number of elements than declared
size t
runtime or at the time of
we to store more elements than declared size then array cannos
array then memory is wasted and if want
l
l
tia
tia
tia
tia
suitable when prior we know exact number of elements to be stored.
be expanded. Array is only
n
en
en
en
declared as array variable by following statement
de
Queue canbe of
fid
fid
fid
int queue[max_queue_size); char stack[max_queue_size]:
on
on
on
max queue size is maximum number of elements inserted into
queue.
C
C
Basic operations which are performed on queue are :
(a) Insert: (This is the process of addition of new element onto the queue called as insertion
operation) When an element is added it will add to the rear end of queuel At that time rear
l
l
ia
ia
ia
ia
value is increased by one and then the element is inserted. Before inserting the element one
nt
nt
nt
nt
must test whether there is room in the queue for the new item{1f there is no space to store
de
e
id
id
id
element in queue means queue is full or overlow
f
f
on
on
on
(b) Delete: This is the process of deletion of an existing element from the front end of queue
C
means queue
ia
ia
ia
ia
is underflow or empty)
nt
nt
nt
nt
id
id
on
on
l
tia
tia
tia
tia
s0 Prashant Publications
en
en
en
en
fi
fi
fi
on
on
on
F&R are set to Zero,
C
1. | Is queue already full?]
if R>= N
Then write("Queue Overflow")
l
l
ia
tia
tia
tia
Return
nt
en
en
en
2. [Increase rear by I]
de
fid
fid
fid
RER+ |
on
on
3
QR] ITEM
C
C
4 [Is front pointer property 1]
IfF =0
l
l
F I
Then
tia
tia
tia
tia
Return
n
en
en
en
de
agorithm to delete an element from queue - Initially underflow condition is checked. If queue
fid
fid
fid
becomes empty then deletion
of element cannot be performed and appropriated error message is return.
on
on
on
Procedure QDELETE(Q,F,R).
C
C
This procedure removes an ITEM from front pointer of queue Q and returns the element. F&R are
the pointers pointing to front and rear position of queue resp.
[ Is queue empty?]
l
l
ia
ia
ia
ia
If F =0
nt
nt
nt
nt
de
id
id
Return
f
f
on
on
on
ITEM QF]
3 [Increase F by 1]
l
l
ia
ia
ia
ia
4. [Finished]
nt
nt
nt
nt
Return
de
e
id
id
id
f
f
on
on
on
Circular Queue:
rear end and deletion take place from front end. When
linear queue, insertion take place from
C
In
space is created there and even if the queue has
Some elements are deleted from queue then empty
l
tia
tia
tia
tia
en
en
en
en
fi
fi
fi
on
on
on
This
storage.
C
further
Means space is not utilize for
empty cells we cannot insert any new element there. space at front end. then
rear full
is but there is
problem is solved in case of circular queue. Even if the overflows. In circular
queue after
queue
the element can be inserted in the beginning nodes until
l
l
make circle.
ia
tia
tia
tia
first element to
pointing the last element of queue, rear points to the
nt
en
en
en
de
fid
fid
fid
FRONT AR
on
on
on
Circalar Qugag ing Linkd
C
C
RE AR
A2) FRONT
l
l
tia
tia
tia
tia
A[2]
n
en
en
en
operation on circular queue
:
de
fid
-
fid
circular queue In fist step overflow
Algorithm to insert an element in message is retum.
and appropriated eror
on
on
on
cannot be performed
queue becomes overflow then insertion
C
C
Procedure CQINSERT(Q,N,F,R,ITEM). by vector
an ITEM onto rear position of queue. Queue is represented
This procedure inserts
to the front and rear elements
of Q. Initia
F and R are the pointers pointing
containing N elements.
l
l
ia
ia
ia
ia
F&R are set to zero.
nt
nt
nt
nt
[Is queue already ful1?]
de
e
id
id
id
If F=l and R=N or if F=R+1
f
f
on
on
on
Return
2 [Find new value of R]
If F=0
l
l
ia
ia
ia
ia
nt
nt
nt
Else If R=N
de
Then R¢I
id
id
id
f
Else RER+1
on
on
on
Q(R]
€ ITEM
4. Return
l
l
tia
tia
tia
tia
62 Prashant Pubications
en
en
en
en
fi
fi
fi
on
on
on
LAHgorithm to delete
an element
from circular queue -
C
If circular queue becomes Initially underflow condition is checked.
empty then
deletion of element
message is return. cannot be performed and
appropriated error
Procedure CQDELETE
(Q,F,R).
l
l
ia
tia
tia
tia
This procedure removes an
ITEMfrom front
nt
en
en
en
the pointers pointing pointer of queue Q
and returns the element. F&R are
to front and rear position
de
of queue resp.
fid
fid
fid
1. [Is queue empty?]
on
on
on
If F=0
C
C
Then write ("Queue Underflow")
Return
2. (Assign front element to ITEM
l
l
tia
tia
tia
tia
ITEM Q[F]
n
en
en
en
[Find new value
de
3. of front]
fid
fid
fid
If F
=R
on
on
on
Then F -0 and R=0
C
C
Else IfF = N
Then
FI
Else
l
l
ia
ia
ia
ia
+ |
FEF
nt
nt
nt
nt
de
4. [Finished]
e
id
id
id
f
f
Return
on
on
on
1
C
other end.
one end and remove from the
ia
ia
ia
ia
nt
nt
nt
representation of deque.
de
id
id
f
rear
on
on
on
|front
insertion
C
deletion
deleti on
insertion
l
tia
tia
tia
en
en
en
en
fi
fi
fi
on
on
on
C
C
are
Basic operations that can be performed on deques
:
l
ia
tia
tia
tia
an item from rear end
b)Jnsert
nt
en
en
en
Delete an item from front end
de
c)
fid
fid
fid
d) Delete an item from rear end in
deque, one
list. There are two variations
array or link
on
on
on
The DeQue can be constructed by using restricted deque allows
one is output restricted deque. Input
and the second
C
C
is input restricted deque at both ends. Output restricted deque
of array or list but deletions allowed
insertions at only one end
one the array or list but insertions allowed at both ends.
end of
allows deletions at only
l
l
Prlority queue
tia
tia
tia
tia
:
en
en
en
de
fid
fid
first before any element of lower priority.
priority is processed
An element wth highest
on
on
on
a) which they
same priority are processed according to the order in
b) Two or more elements with
C
C
were added to the queue.
queye.
Such type of queue is called priority
queues Priority Queue 2.Desceding priorityqueue.
priority
:
1.Ascending
There are two types of
l
l
ia
ia
ia
ia
and
queue is a collection of items into which items can be inserted arbitrarily
Ascending priority
nt
nt
nt
nt
can be removed first.
from which only the smallest item
de
e
id
id
id
In Descending priority queue allows deletion of only
the largest item.
f
f
on
on
on
queue.
Stack may be viewed as descending priority
queue.
C
a)
In CPU.scheduling shortest job is given the highest-prierity toprocess over the longer jobs.
l
ia
ia
ia
a
b)
An
routine type job.
nt
nt
nt
nt
de
e
id
id
id
f
f
on
on
on
C
C
l
l
tia
tia
tia
tia
en
en
en
en
fi
fi
fi
Following pictures
on
on
on
are two ways to
None fixed
C
1.
size of the queues: dotwoqueucksn array
Grap
hicalPicture:
wthout fuxed
size ofthe queue
l
l
ia
tia
tia
tia
Aray_tr
nt
en
en
en
de
-6 5
fid
fid
fid
4 3 -2
on
on
on
5 2 3 4
7
S
C
C
Count Front
Count
Size Front
Size
l
l
tia
tia
tia
tia
2 2 2
1
Queue
ue ue 1 1 Queue 1
Queue ue ue
n
en
en
en
Que Que
Que
Que
de
Temporary
fid
fid
fid
Boundary
on
on
on
C
C
a) Queue I
expands from the 0" element to the right and circular
back to the element O
Queue 2 expands from the 8" element to the left and circular back to
the 8 element
C) Temporary boundary between the Queue I and the Queue 2; as long as there has
l
l
free
ia
ia
ia
ia
elements in the array and boundary would be shif.
nt
nt
nt
nt
Free elements could be anywhere in the Queue such as before the front, after the rear, and
de
e
id
id
id
between front and rear in the Queue.
f
f
on
on
on
) Queue 1's and Queue 2's size could be change if it is necessary. When the Queue I is full
C
Elements -I, -2, and -3 are using to store the SIze the
of
)
l
l
ia
ia
ia
ia
I.
needed to manipulate the Queue
and the data count for the Queue
I
nt
nt
nt
nt
id
id
f
on
on
on
C
65
l
Datu Seracture
:
CS- 23l
tia
tia
tia
tia
en
en
en
en
fi
fi
fi
on
on
on
2. Fixed size of the queue:
C
Graphical Plc ture: with ixed
aze ofthe queue
Army_ ptr
l
l
ia
tia
tia
tia
1 2 3 4 5
nt
4 -3 -2 1 0
en
en
en
de
4 0
fid
fid
fid
on
on
on
C
C
Sie Queue 2
Queue 1
1
e
Que Boundarybetwe en
the Que ues
l
l
tia
tia
tia
tia
n
en
en
en
element to the 4h element and
circular back to 0" element
de
0
Queue 1 expands from the
fid
fid
fid
a)
back to 8" element
Queue 2 expands from the 8" element to the s" element and circular
on
on
on
b)
I andthe Queue 2
c) The boundary is fixed between the Queue
C
C
in the Queue such as before the front, after the rear, and
d) Free elements could be anywhere
between front and rear in the Queue
of the Queue I.
Elements -1, -2, and -3 are using to store the size of the Queue I, the front
l
l
e)
ia
ia
ia
ia
I
and the data count for the Queue I needed to manipulate the Queue
nt
nt
nt
nt
Elements -4, -5, and -6 are using to store the size of the Queue 2, the front
de
e of the Queue 2.
id
id
id
and the data count for the Queue 2 needed to manipulate the Queue 2
f
f
on
on
on
C
tpplications of queues:
Used in time sharing systemin which programs
() with the same priority form a queue while
waiting to be executed.
l
l
ia
ia
ia
ia
(i) Used for performing level order traversal of a binary tree and for performing breadth firs
nt
nt
nt
nt
search at a graph.
de
e
id
id
id
f
on
on
on
l
tia
tia
tia
tia
66 Prashant Publications
en
en
en
en
fi
fi
fi
on
on
on
C
C
(v) Simulation (Queuing Theory)
l
l
S
ia
tia
tia
tia
(vi) Input output buffers.
nt
en
en
en
(vi) Graph Searching.
de
fid
fid
fid
on
on
on
Questions:
C
C
1. How is the queue different from stack?
l
tia
tia
tia
tia
3.
memory of computer?
What is queue? How it is implemented in
n
en
en
en
4.
de
fid
fid
5.
on
on
on
6. What is priority queue?
on circular queue.
C
C
insertion and deletion operations
7. Write an algoritham for
queue over linear queue?
8. Whar are the advantages of circular
l
l
ia
ia
ia
ia
9. Define Queue.
nt
nt
nt
nt
Give real world example of Queue.
de
10.
e
id
id
id
List the operations on Queue.
f
f
11.
on
on
on
on Queue.
13. Define Insertion operation
14. Define Deletion operation on Queue.
l
l
ia
ia
ia
ia
nt
nt
nt
nt
id
id
l6. of linear
circular queue.
f
f
on
on
on
l
tia
tia
tia
tia
en
en
en
en