2. 6-2
Introduction
Queue: a collection whose elements are added at
one end (the rear or tail of the queue) and
removed from the other end (the front or head of
the queue)
A queue is a FIFO (first in, first out) data structure
3. 6-3
Conceptual View of a
Queue
Front of queue
Adding an element
New element is added to
the rear of the queue
4. 6-4
Conceptual View of a
QueueRemoving an element
Element is removed from
the front of the queue
5. Components of Queue
âą Front is a variable which refers to first position in
queue.
âą Rear is a variable which refers to last position in
queue.
MaxQueue is variable that
describes
maximum number of elements in a queue.
6. 6-6
Uses of Queues in Computing
For any kind of problem involving FIFO data
Printer queue
Keyboard input buffer
7. Queue Operation
Two basic operations are associated with queue:
1.âInsertâ operation is used to insert an element into a
queue. (From the rear)
2. 2. âDeleteâ operation is used to delete an element
from a queue. (From the Front)
8. Steps in Insertion operation:
âąQueue can be added when itâs not full
âąIf queue is empty then front and rear is added
by 1. For the contrary, rear is added by 1.
Insertion Operation
10. Algorithm of Insertion
Algorithm-Insertion
Write an algorithm to add an âitemâ in a queue âQâ.
1.IF R=N THEN
Print âOverflowâ
Return
Else
R=R+1 [End of IF Structure]
2.[Insert Item]
Q[R]=âitemâ
IF F=0 THEN
F=1
END IF
11. Steps in deletion operation:
âąQueue of element can be deleted when its
elements is not empty.
âąWhen the element is deleted from the queue
then subtract the rear pointer with 1.
Deletion
12. Algorithm for deletion
Algorithm-Insertion
Write an algorithm to add an âitemâ in a queue.
Step 1:Â If FRONT = 0 then
Write âQueue is Underflowâ          Â
Step 2:Â Delete QUEUE [FRONT]
Step 3:Â If FRONT = REAR then
FRONT = 0Â Â Â Â Â Â Â Â Â Â Â
REAR = 0Â Â Â Â Â Â Â Â Â Â Â
Else          Â
FRONT = FRONT + 1Â Â Â Â Â Â Â Â Â Â Â
Step 4: Exit
13. 3
Figure: Circular Queue having
Rear = 5 and Front = 0
Drawback of Linear Queue
âąOnce the queue is full, even though few elements from the front are deleted and
some occupied space is relieved, it is not possible to add anymore new elements,
as the rear has already reached the Queueâs rear most position.
Circular Queue
âąThis queue is not linear but circular.
âąIts structure can be like the following figure:
âąIn circular queue, once the Queue is full the
"First" element of the Queue becomes the
"Rear" most element, if and only if the "Front"
has moved forward. otherwise it will again be
a "Queue overflow" state.
14. Example: Consider the following circular queue with N = 5.
1. Initially, Rear = 0, Front =
0.
3. Insert 50, Rear = 2, Front = 1.
Rear
14
Rear
2. Insert 10, Rear = 1, Front = 1.
Rear Front
Front
4. Insert 20, Rear = 3, Front = 1.
Front
Rear
5. Insert 70, Rear = 4, Front = 1.
Front
Rear
6. Delete front, Rear = 4,
Front = 2.
Front
15. 7. Insert 100, Rear = 5, Front = 2.
9. Insert 140, Rear = 1, Front = 2.
As Front = Rear + 1, so Queue overflow.
Front
Rear
8. Insert 40, Rear = 1, Front =
2.
Rear Front
Rear
10. Delete front, Rear = 1, Front = 3.
Rear
Front
Front
11. Delete
front, Rear =
1, Front = 4.
Rear
15
Front
12. Delete front, Rear = 1,
Front = 5.
Rear
Front
16. Algorithm Insertion For Circular Queue
Algorithm-insertioncq
The algorithm inserts an element X into queue âQâ having N elements. F and R
represent the pointers to the front and Rear element of the queue.
1.[Check Overflow]
IF (F=1 and R=N ) or (F=R+1)THEN
Print âOverflowâ
Return [End of IF Structrue]
2.IF (F=0 and R=0) THEN
F=1
R=1
Else IF R=N THEN
R=1
Else R=R+1 [End of IF Structure]
Q[R]=X [Insert Element X]
3. Exit
17. Algorithm Deletion For Circular Queue
Algorithm-Deletioncq
The algorithm delete an element X into queue âQâ having N
elements. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check whether or not The Queue is empty]
IF F=0 THEN
Print âQueue is Emptyâ
Return [End of IF Structrue]
2. [Delete Element]
Delete Q[F]
3. IF F=R THEN
F=R=0
Else IF F=N THEN
F=1
Else F=F+1 [End of IF Structure]
3. Exit
18. Double Ended Queue
The deque is pronounced as de-queue. It is a linear structure
in which items can be added or removed at either end.
However, an item cannot be added or deleted in the middle
of the deque. Since items can be added or deleted at both
ends of the queue.so it is called double ended queue. The
deque is represented as shown in the following figure
19. Types of Deque
There are two types of deque depending upon the restriction
to perform insertion or deletion operations at the two ends.
1. Input restricted deque
An input restricted deque is a deque, which allows
insertion at only 1 end, rear end, but allows deletion at
both ends, rear and front end of the lists
2. Output restricted deque
An output-restricted deque is a deque, which
allows deletion at only one end, front end, but allows
insertion at both ends, rear and front ends, of the lists.
20. The possible operation performed on
deque
1.Add an element at the rear end
2.Add an element at the front end
3. Delete an element from the front end
4. Delete an element from the rear end
ïOnly 1st
, 3rd
and 4th
operations are performed by input-restricted
deque
ï1st, 2nd
and 3rd
operations are performed by output-restricted
deque.
20
21. Algorithm For insertion at rear end in Deque
Algorithm-insertiondq
The algorithm is used to inserts an element âitemâ into queue âDQâ having N
elements. F and R represent the pointers to the front and Rear element of the
queue.
1.[Check Overflow]
IF F=1 and R=N THEN
Print âOverflowâ
Return [End of IF Structrue]
2.IF (F=0 and R=0) THEN
F=1
R=1
Else IF R=N THEN
R=1
Else R=R+1 [End of IF Structure]
DQ[R]=X [Insert Element X]
3. Exit
22. Algorithm For insertion at Front end in Deque
Algorithm-insertiondq
The algorithm is used to inserts an element âitemâ into queue âQâ having N
elements from front end. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check Overflow]
IF F=1 and R=N THEN
Print âOverflowâ
Return [End of IF Structrue]
2.IF (F=0) THEN
F=1
R=1
Else IF (F=1) THEN
F=N
Else F=F-1 [End of IF Structure]
DQ[F]=X [Insert Element X]
3. Exit
23. Algorithm For deletion from Front end in Deque
Algorithm-deletiondqfront
The algorithm is used to inserts an element âitemâ into queue âDQâ having N
elements from front end. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check Overflow]
IF F=0 THEN
Print âunderflowâ
Return [End of IF Structrue]
2. Delete DQ[F]
3. IF (F=R) THEN
F=0
R=0
Else IF (F=N) THEN
F=1
Else F=F+1 [End of IF Structure]
3. Exit
24. Algorithm For deletion from rear end in Deque
Algorithm-deletiondqrear
The algorithm is used to inserts an element âitemâ into queue âDQâ having N
elements from front end. F and R represent the pointers to the front and Rear
element of the queue.
1.[Check Overflow]
IF R=0 THEN
Print âunderflowâ
Return [End of IF Structrue]
2. Delete DQ[R]
3. IF (R=F) THEN
F=0
R=0
Else IF (R=1) THEN
R=N
Else R=R-1 [End of IF Structure]
3. Exit