Queue : Linear Data Structure
Smt. K. D. Patel Department of Information
Technology, CSPIT, CHARUSAT
Prepared By : Dr. Purvi Prajapati
Queue
Queue Data Structure 2
Applications of Queue
single-lane one
way road
where the
vehicle enters
first, exits first
Passengers in queue
at Railway station, Bus
stop, Movie ticket
counter, etc.
Queue Data Structure 3
Applications of Queue
• Resource Sharing
Queue Data Structure 4
Applications of Queue
Networking
• Packet transmission
Queue Data Structure 5
Queue
Output A B C D Input
Queue Data Structure 6
What Is Queue
• Queue is a linear data structure
• First In First Out (FIFO)
• Adding an entry at the rear
• Deleting an entry at the front
Deleting Adding
A B C
front rear
Queue Data Structure 7
Queue
• Queue is FIFO ( First-In First-Out)
• A queue is open at two ends.
• add entry (enqueue) at the rear
• delete entry (dequeue) at the front.
Note that you cannot add/extract entry in the
middle of the queue.
Queue Data Structure 8
Operations
• Enqueue()
• add a new item at the rear
• Dequeue()
• remove a item from the front
• isEmpty()
• check whether the queue is empty or not
• isFull()
• check whether the queue is full or not
• Size()
• return the number of items in the queue
• Peek()
• return the front item without removing it.
Queue Data Structure 9
Example of Queue
q = new QueueImpl() q []
[Link](a) q [a]
[Link](b) q [a,b]
[Link](c) q [a,b,c]
[Link](d) q [a,b,c,d]
[Link]() a q [b,c,d]
[Link]() b q [c,d]
[Link](e) q [c,d,e]
[Link]() c q [d,e]
[Link]() d q [e]
Elements of a queue are processed in the same order as
the they are inserted into the queue, here “a” was the
first element to join the queue and it was the first to
leave the queue: first-come first-serve.
Queue Data Structure 10
Printing Queue
• [Link] [Link] [Link] arrive to printer.
C B A Now printing [Link]
C B [Link] is finished. Now printing [Link]
[Link] comes D C B Now still printing [Link]
D C [Link] is finished. Now printing [Link]
D [Link] is finished. Now printing [Link]
Queue Data Structure 11
Representation of Queue
• Two ways to represent/implement a queue in
memory:
1) Using Array (Static Queue)
2) Using Linked List (Dynamic Queue)
Queue Data Structure 12
Array Representation of Queue
n-1 3 2 1 0
DCBA
Max_Size rear front
After A leaves,
n-1 3 2 1 0
DCB
Max_Size rear front
Queue Data Structure 13
Static Queue Operations
Enqueue(Q, F, R, N, Y) : Given F and R, pointers to the front and rear
elements of a queue, a queue Q consisting of N elements, and an element Y,
this procedure inserts Y at the rear of the queue. Initially, F and R have been
set to (-1).
1. [Overflow?]
If R = = (N-1)
then Write (“Queue OVERFLOW”)
Return
2. [Increment rear pointer]
R=R+1
3. [Insert element]
Q [R] = Y
4. [Is front pointer properly set?]
If F == (-1)
then F = 0
Return
Queue Data Structure 14
Static Queue Operations
Dequeue (Q, F, R). Given F and R, the pointers to the front and rear elements of a
queue, respectively, and the queue Q to which they correspond, this function deletes
and returns the last element of the queue. Y is a temporary variable.
1. [Underflow?]
If F == (-1)
then Write (“Queue UNDERFLOW”)
Return (-1) //-1 denotes an empty queue
2. [Delete element]
Y = Q [F]
3. [After Deletion, Is Queue empty? ]
If F = = R
F = (-1)
R = (-1)
else
F=F+1
4. [Return Deleted element]
Return (Y) Queue Data Structure 15
Dynamic Queue Operations
Enqueue(F, R , x) : F is the front pointer to the first (front most) element
of the Queue and R is the rear pointer to last element of the Queue.
Insert Y at the rear end of the queue. Initially, F and R are NULL.
1. Create New Node : Temp (allocate memory)
INFO(Temp) = x; LINK(Temp) = NULL;
2. [Is Queue empty?]
If(F==NULL && R = = NULL)
F=Temp; R=Temp;
Return
3. Insert new node at Rear end
Link(R) = Temp;
4. Set rear at the new node
R = Temp;
Queue Data Structure 16
Dynamic Queue Operations
Dequeue (F, R) : this function deletes First (front most) element of the Queue. Y is
a deleted element.
1. [Underflow?]
If F == NULL
then Write (“Queue is empty”)
Return
2. [Delete element]
Y = INFO(F)
3. [After Deletion, Is Queue empty? ]
If (F = = R)
F = NULL
R = NULL
else
F = LINK(F)
4. [Return Deleted element]
Return (Y)
Queue Data Structure 17
Problem
• An array has limited size, once rear is at the end of
this array, and there is new item coming in, what
can we do?
n-1 3 2 1 0
Y X ……
rear front
Queue Data Structure 20
Two Solutions
• Shifting all items to front in the array when
dequeue operation. ( Too Costly… )
n-1 3 2 1 0 A leaves n-1 3 2 1 0
…… CBA …… CB
rear=3 front=0 rear=2 front=0
• Wrapped around array ---- Circular Array
Queue Data Structure 21
• Wrapped around array
Circular Array
rear=3
3
2
C
n-1 3 2 1 0 front=0 1
B
…… CBA
0 A
rear=3 front=0 n-1
Queue Data Structure 22
EnQueue & DeQueue In Circular Array
• EnQueue • DeQueue
3 rear=3 front=1 3
2 2
1 BC 1 BC
0 A 0
n-1 n-1
Queue Data Structure 23
CIRCULAR QUEUE
Procedure CQ_Enqueue (F, R, Q, N, Y). Given pointers to the front and rear elements of a
circular queue, F and R, an array Q consisting of N elements, and an element Y, this procedure
inserts Y at the rear of the queue. Initially, F and R are set to (-1)
1. [Reset rear pointer?]
If R == N-1
then R = 0
else R = R + 1
2. [Overflow?]
If F == R
then Write (“OVERFLOW”) //Update R to its previous position
If R == 0
then R = N-1
else R = R - 1
Return
3. [Insert element]
Q [R] = Y
4. [Is front pointer properly set?]
If F = = (-1)
then F = 0
Return
Queue Data Structure 25
--------------------------------------------------------------------------------------------------------------------------
CIRCULAR QUEUE
Function CQ_Dequeue( F, R, Q, N). Given F and R, pointers to the front and rear
elements of a circular queue, respectively, and an array Q consisting of N elements, this
function deletes and returns the last element of the queue. Y is a temporary variable.
1. [Underflow?]
If F == (-1)
then Write (“UNDERFLOW”)
Return(-1)
2. [Delete element]
Y = Q [F]
3. [Queue empty? ]
If F == R
then F = (-1)
R= (-1)
Return (Y)
4. [Increment front pointer]
If F == N-1
then F = 0
else F = F + 1
Return (Y)
Queue Data Structure 26
Example
• Show circular queue contents with front and rear
after each step with size=5.
(i) Insert 10, 20, 30.
(ii) Delete
(iii) Insert 40, 50, 60, 70.
Queue Data Structure 28
Example
(i) Insert 10, 20, 30
1
After Insert 10 R=1, F=1
5
10 After Insert 20 R=2, F=1
After Insert 30 R=3, F=1
20 2
4
30
Delete After delete R=3, F=2
5 1
20 2
4
30
Insert 40, 50, 60, 70 After Insert 40 R=4, F=2
After Insert 50 R=5, F=2
5 1 After Insert 60 R=1, F=2
50 60 After insertion of 60 circular queue is full. So, when 70 is
tried to be inserted it will be OVERFLOW. So, 70 can’t be
inserted.
20 2
4 40
30
Queue Data Structure 30
3
Customer Service In Royal Bank
• Suppose there is only one customer service available
in Royal Bank on Saturday morning at 8:00 clock.
• In every 3 minutes, a new customer arrives at the
end of waiting line
• Each customer will need 5 minutes for the service
• Print out the following information of queue at 8:30
am
• The time of arriving and leaving for each customer
• How many customers are in the line?
• Who is the current serving customer?
Queue Data Structure 31
Queue Data Structure 32
Queue Data Structure 33
Queue Data Structure 34
Deque
• Unlike a queue, in deque, both insertion and
deletion operations can be made at either end of
the structure. Actually, the term deque has
originated from double ended queue.
Front Rear
Deletion Deletion
…………...
Insertion Insertion
• It is clear from the deque structure that it is a
general representation of both stack and queue.
• In other words, a deque can be used as a stack as
well as a deque. Queue Data Structure 35
Operations
• The following four operations are possible on a
deque which consists of a list of items:
• Insert(item): to insert item at the Front end of a
deque.
• Deque(): to remove the Front item from the deque.
• Enqueue(item): to inert item at the Rear end of a
deque.
• Eject(): to remove the Rear item from a deque.
Queue Data Structure 36
Variations of Deque
• There are two known variations of deque:
[Link]-restricted deque.
[Link]-restricted deque.
Front Rear
Insertion
Deletion …………...
Deletion
Input-restricted deque
Front Rear
Insertion
…………... Insertion
Deletion
Output-restricted deque
An input-restricted deque is a deque which allows insertions
at one end(rear) only, but allows deletions at both ends.
An output-restricted deque is a deque where deletions take
place at one end only(front), but allows insertions at both
ends. Queue Data Structure 37
Priority Queue
• A priority queue does not strictly follow the first-in-
first-out (FIFO) principle which is the basic principle
of queue.
1. An element of higher priority is processed before
any element of lower priority.
2. Two elements with the same priority are
processed accordingly to the order in which they are
added to the queue (FIFO).
Queue Data Structure 39
Priority Queue
• Two queues
• one is high priority queue
• the other is low priority queue
• Service rules:
• First serve the people in high priority queue
• If no passengers are in high priority queue, serve the
passengers in low priority queue
Queue Data Structure 40
Air Travel
• Only one check-in service at airport
• Two waiting lines for passengers
• one is First class service
• the other is Economy class service
• Passengers in the first-class waiting line have higher
priority to check in than those in the economy-class
waiting line.
Queue Data Structure 41
Two Queues
• High Priority Queue, will come in hpQue
• Low Priority Queue, will come in lpQue
High Priority Queue
Customers coming in
HDC
Check In
GF EBA
Low Priority Queue
Queue Data Structure 42
Types of Priority Queue
Queue Data Structure 43
Priority Queue
• There are various ways to implement Priority
Queue :
1. Using Simple array/ Circular array
2. Multi-queue implementation
3. Using linked list
4. Using heap tree
Queue Data Structure 44
Priority Queue Using Array
Queue Data Structure 45
Multi-queue implementation
F R
● ●
Priority p1
F R
● ●
Priority p2
F R
● ●
Priority pn
Queue Data Structure 46
Priority Queue Using linked list
• Enqueue() : This function is used to insert a new
data into the dynamic queue (using SLL).
• Dequeue() : This function removes the element
with the highest priority from the dynamic queue
(using SLL).
Queue Data Structure 47
Example
Perform Following operations on Priority Queue
using Simple Queue. Size = 6
Person1-4, Person2-3, Person3-5, Person4-2,
Delete,Delte,Person5-1,Person6-2,Person7-5, Delete,
Delete, Delete, Delete
(Here 1 indicate higher priority)
Queue Data Structure 48
Applications of Queue
Operating System
• Handling of interrupts in real-time systems. The
interrupts are handled in the same order as they
arrive i.e First come first served.
• Data transmission between two processes.
Examples include IO Buffers, pipes, file IO, etc.
Queue Data Structure 49
Applications of Queue
• CPU Scheduling: Operating systems often maintain
a queue of processes that are ready to execute or
that are waiting for a particular event to occur.
Queue Data Structure 50
Applications of Queue
• Resource Sharing
Queue Data Structure 51
Applications of Queue
Networking (Queues in routers/ switches)
Packet transmission
Queue Data Structure 52
Applications of Queue
• Instruction Pipelining
Queue Data Structure 53
Applications of Queue
• Round Robin Scheduling in Operating System
Queue Data Structure 54
Applications of Queue
• BFS : Breath First Traversal in Graph
Queue Data Structure 55
Applications of Queue
• Buffer Management
Queue Data Structure 56
Applications of Queue
• Traffic Management: Queues can be used to
manage traffic flow in transportation systems, such
as airport control systems or road networks.
• Traffic software ( Each light gets on one by one
after every time of interval of time using circular
queue)
Queue Data Structure 57
Applications of Priority Queue
• Graph Algorithms:
• Shortest Path
• Minimum Spanning tree
• Data compression in Huffman code
• CPU Scheduling : Priority Scheduling (OS), Task
scheduling based on priority
• Optimization Problems
Queue Data Structure 58
Applications Priority Queue
• Priority Scheduling
Queue Data Structure 59
Applications of Queue : Deque
• Palindrome checker
Queue Data Structure 60
Applications of Queue : Deque
• Maximum of all subarrays of size k problem
• 0-1 BFS
• Find the first circular tour that visits all petrol
pumps
Queue Data Structure 61
• Compare Stack and Queue.
Queue Data Structure 62
Linear Data structures
Array Stack Queue
Random LIFO FIFO
Access (Last In First Out) (First In First Out)
Index is TOP pointer is used to FRONT and REAR pointer
used to access top most is used to access queue
access array element of the stack elements
elements
Only one end is used Two end : FRONT end is
for insertion and used for deletion and
deletion. REAR end is used for
Push() : Insertion insertion.
Pop() : Deletion Enqueue() : Insertion
Dequeue() : Deletion
Queue Data Structure 63
Thank You
Queue Data Structure 64