Queue
Introduction
• A queue is a non-primitive linear data structure.
• It is a homogeneous collection of elements in
which new elements are added at one end called
the Rear end and the existing elements are
deleted from another end called the Front end.
• A queue is logically a First In First Out (FIFO)
type of list.
• In our everyday life we come across many
situation where we are wait for the desired
service, there we have to get into a waiting line
for our turn to get serviced.
Introduction
• This waiting queue can be thought of as a Queue. This
Queue means a Line.
• For example:
• At the railway reservation booth, we have to get into
the reservation queue.
• Here, the important feature of the queue is when new
customers got into the queue from the rear end,
whereas the customers who get their sets reserved
leave the queue from the front end.
• It means the customers are serviced in the order in
which they arrive the service center.
• This same characteristics apply to our Queue.
Operations on Queue
• Enqueue: Adds a new element to
the queue.
• Dequeue: Removes and returns
the first (front) element from the
queue.
• Peek: Returns the first element
in the queue.
• isEmpty: Checks if the queue is
empty.
• Size: Finds the number of
elements in the queue.
Introduction
• The following figures show queue
graphically during insertion
operation's=Front & R=Rear
(a)Empty
Queue 2 3
0 1 4
F=-1 & R=-1 F R
(b) One element 10
inserted in queue 0 1 2 3 4
F=0 & R=0
F R
(b) Second element
inserted in queue 10 20
F=0 & R=1 0 1 2 3 4
F R
Introduction
• The following figures show queue graphically
during deletion operation's=Front & R=Rear
(b) Second element
inserted in queue 10 20
0 1 2 3 4
F=0 & R=1
F R
(b) One element deleted
from queue 20
F=1 & R=1 0 1 2 3 4
F R
(a)Empty
Queue
0 1 2 3 4
F=-1 & R=-1
F R
Queue
Implementation
• There are two ways to implement a
Queue:
• Static implementation (using array)
• Dynamic implementation (using Pointer/Linked
list)
Static Implementation
(using array)
• If queue is implemented using arrays, we must be sure about
the exact number of elements .
• We want to store in the queue, because we have to declare the
size of the array at design time or before the processing starts.
• In this case, the beginning of the array will become the front
for the queue and the last location of the array will act as rear
for the queue.
• The following relation gives the total number of elements
present in the queue, when implemented using arrays:
• Total no of elem.=front-rear+1
• Also note that if rear<front then there will be no element in the
queue or queue is always be empty.
Static Implementation
(using array)
Dynamic implementation
(using Pointer/Linked list)
• Implementing queues using
pointers, the main disadvantage is
that a node in a linked
representation occupies more
memory space then a corresponding
element in the array representation.
Types of Queue
• There are many different way to implement a
Queue:
• Simple Queue/Linear Queue - A linear data
structure where items are deleted in the same order
they were added.
• Circular Queue - A linear data structure where the
front and rear ends are connected to form a circle.
• Double Ended Queue (Deque) - Allows items to be
inserted and deleted from both ends.
• Priority Queue - An element's priority determines
when it is dequeued, not the order it was added.
Simple Queue
• In the simple queue, the beginning of
the array will become the front for
the queue and the last location of the
array will act as rear for the queue.
• The following relation gives the total
number of elements present in the
queue, when implemented using
arrays:
Total no of elem.=front-rear+1
Simple Queue
• Also note that if rear<front then
there will be no element in the queue
or queue is always be empty.
• Below show a figure a empty simple
queue Q[5] which can accommodate
five elements.
Way of Way of
deletion Q[0] Q[1] Q[2] Q[3] Q[4] insertion
Fig: Simple
Queue
Operations on Queue
1. Insertion : Placing an item in a queue is
called “insertion or enqueue”, which is done
at the end of the queue called “rear”
2. Deletion : Removing an item from a
queue is called “deletion or dequeue” ,
which is done at the other end of the queue
called “front”.
Algorithm for Enqueue
(insert an element in queue)
Algorithm Enqueue(QUEUE, SIZE, ITEM)
{
STEP-1 IF REAR= MAX-1 //Queue is full
write OVERFLOW go to step 4
[end of if]
STEP-2 if REAR = -1 and FRONT = -1 //Queue
is empty
set FRONT=REAR= 0 else set REAR = REAR+1
STEP-3 set QUEUE [ REAR ] = NUM
STEP-4 EXIT
}
Algorithm For Dequeue
(Delete an element from
queue)
Algorithm Dequeue(QUEUE, ITEM){
STEP-1 If FRONT = -1 or FRONT > REAR
write UNDERFLOW
Else set ITEM = QUEUE [ FRONT ]
If ( FRONT = REAR )
REAR = -1
FRONT = -1
set FRONT = FRONT + 1
[end of if]
[end of if]
STEP-2 EXIT }
Difference Between
Stack and Queue
Queue Animation
• https://www.cs.usfca.edu/~galles/visuali
zation/QueueArray.html
Simple Queue
• The Array below is used as a Queue data
structure:
• [8,10,5,4]
• Which indexes and values are affected by
the enqueue and dequeue operations?
• enqueue(9): value 9 is placed on index __
in the array.
• dequeue(): value __ is taken out of the
queue.
Limitation of Simple
Queue
• There are certain problems associated with
a simple queue when queue is implemented
using array.
• Consider an example of a simple queue
Q[5], which is initially empty.
• We can only insert five elements in queue.
• If we attempt to add more elements, the
elements can’t be inserted because in a
simple queue rear increment up to size-1.
Limitation of Simple
Queue
• At that time our program will flash the
message “Queue is full”.
• On deletion of an element from existing
queue, front pointer is shifted to next
position.
• This results into virtual deletion of an
element.
• By doing so memory space which was
occupied by deleted element is wasted and
hence inefficient memory utilization is occur.
Circular Queue
1.https://www.geeksforgeeks.org/introduction-to-circular-queue
/
2.https://www.youtube.com/watch?v=BP_Ug-cJMRE
Circular Queue
• There was one limitation in the array implementation of
Queue.
• If the rear reaches to the end position of the Queue then
there might be possibility that some vacant spaces are left in
the beginning which cannot be utilized. So, to overcome such
limitations, the concept of the circular queue was introduced.
• Circular queue is a linear data structure. It follows FIFO
principle.
• In circular queue the last node is connected back to the first
node to make a circle.
• A circular queue is one in which the insertion of a new
element is done at the very first location of the queue if the
last location of the queue is full.
Circular Queue
• In other words, if we have a queue Q of say n
elements, then after inserting an element last
(n-1th) location of the array the next elements
will be inserted at the very first location (0) of
the array.
• It is possible to insert new elements, if and
only if those location (slots) are empty.
• A circular queue also have a Front and Rear to
keep the track of the elements to be deleted
and inserted and therefore to maintain the
unique characteristic of the queue.
Circular Queue
• Below show a figure an empty circular
queue Q[5] which can accommodate five
elements.
Q[0] Q[1]
Q[4] `
Q[2]
Q[3]
Fig: Circular Queue
Circular Queue
Algorithm for Enqueue
operation
Algorithms ENQUEUE(Queue, MAX, Front, Rear,
Element)
{
Step 1: If(Front = = (Rear+1)%MAX)
print “Queue is full/OVERFLOW”
Go to step 2
else
{ if Rear = = -1{
then set Front = Rear = 0
Queue[Rear] = Element }
else{
Rear = (Rear +1)%MAX
Queue[Rear] = Element
}}
Step 2: Stop
}
Circular Queue -
Enqueue operation
Circular Queue - Enqueue
operation
Algorithm for Dequeue
operation
Algorithms DEQUEUE(Queue, MAX, Front, Rear)
{
Step 1: if (Front = = - 1)
Print “Queue is empty/UNDERFLOW”
Go to step 2
else{
item=Queue[Front]
if (Front= = Rear)
Set Front = Rear = -1
else
Front = (Front +1)%MAX
Print “Deleted element”
Step 2: Stop
}
Algorithm for Dequeue
operation
Algorithm for Dequeue
operation
Example: Circular Queue
Q. 1 Consider the following circular queue capable of
accommodating maximum six elements: Current position
of both ends are Front = 2, Rear = 4 .
•Describe the queue with diagram as following operations take place.
I.Add O II. Add P III. Delete two letters IV. Add Q, R, S
Applications of
Queues
• Round robin technique for processor scheduling is
implemented using queues.
• All types of customer service (like railway ticket
reservation) center software’s are designed using
queues to store customers information.
• Printer server routines are designed using queues. A
number of users share a printer using printer server
the printer server then spools all the jobs from all
the users, to the server’s hard disk in a queue. From
here jobs are printed one-by-one according to their
number in the queue.
Double Ended Queue
(Deque)
• It is also a homogeneous list of elements in which
insertion and deletion operations are performed from
both the ends.
• That is, we can insert elements from the rear end or
from the front ends.
• Hence it is called double-ended queue. It is commonly
referred as a Deque.
• There are two types of Deque. These two types are
due to the restrictions put to perform either the
insertions or deletions only at one end.
Double Ended Queue
(Deque)
• There are:
• Input-restricted Deque.
• Output-restricted Deque.
• Below show a figure a empty Deque Q[5] which can
accommodate five elements.
Fig: A Deque
deletio inserti
n
inserti on
deletio
on Q[0] Q[1] Q[2] Q[3] Q[4] n
Double Ended Queue
(Deque)
Input-restricted Deque: An input restricted
Deque restricts the insertion of the elements
at one end only, the deletion of elements can
be done at both the end of a queue.
Fig: A representation of an input-
restricted Deque
deletio inserti
n
10 20 30 40 50 on
deletio
Q[0] Q[1] Q[2] Q[3] Q[4] n
F R
Double Ended Queue
(Deque)
Output-restricted Deque: on the contrary, an
Output-restricted Deque, restricts the
deletion of elements at one end only, and
allows insertion to be done at both the ends
of a Deque.
Fig: A representation of an Output-
inserti
restricted Deque inserti
on
10 20 30 40 50 on
deletio
Q[0] Q[1] Q[2] Q[3] Q[4] n
F R
Double Ended Queue
(Deque)
The programs for input-restricted Deque
and output-restricted Deque would be
similar to the previous program of Deque
except for a small difference.
The program for the input-restricted
Deque would not contain the function
addqatbeg().
Similarly the program for the output-
restricted Deque would not contain the
function delatbeg().
Priority Queue
• A priority queue is a collection of elements
where the elements are stored according to
their priority levels.
• The order in which the elements should get
added or removed is decided by the priority
of the element.
• Following rules are applied to maintain a
priority queue.
• The element with a higher priority is processes before
any element of lower priority.
• If there are elements with same priority, then the
element added first in the queue would get processed.
Priority Queue
• Here, greater number that is highest priority
and smallest number that is less priority.
• Priority queues are used for implementing job
scheduling by the operating system.
• Where jobs with higher priorities are to be
processed first.
• Another application of priority queue is
simulation systems where priority corresponds
to event times.
Questions
Q. 1 Explain the representation of a
stack using an array. Illustrate how
push and pop operations are
performed in representations.
Q. 2 Compare stack and queue in
terms of their properties, operations,
and real-world applications.
Q. 3 Consider a circular queue of size 5.
Initially, the queue is empty. Perform the
following operations and describe the queue
with a diagram at each step:
1.Enqueue A, B, C
2.Dequeue one element
3.Enqueue D, E
4.Enqueue F (check for overflow and
explain what happens)
5.Dequeue two elements
6.Enqueue G, H