Data Structures (Queue) : Madhuri Kalani
Data Structures (Queue) : Madhuri Kalani
(QUEUE)
MADHURI KALANI
Why are we studying Data Structures ?
Modern world all about … DATA
Dealing with data…
• How to use it ?
• How to store it ?
• How to process it ?
• How to gain “knowledge” from it ?
• How to keep it secret?
Dealing with data…
• How to use it ?
• How to store it ?
• How to process it ?
• How to gain “knowledge” from it ?
• How to keep it secret?
How should data be stored?
Depends on your requirement
Data is diverse ..
But we have some building blocks
To store our big data
Elementary Data “Structures”
• Arrays head
• Lists
• Stacks 2
1
• Queues 3 4
7 8
• Trees 5 6
F R
In some languages these are basic data types – in others they need
to be implemented
9
Abstract Data Type
A mathematical definition of
objects, with operations defined
on them.
Abstract type (or class) for
objects whose behaviour is
defined by a set of value and a
set of operations.
10
• An abstract data type (ADT) is an
Abstract abstraction of a data structure
11
Queues
12
QUEUES
• Queue is a Linear data
structure.
• It follows the
principle of FIFO :- First
In First Out.
• There are 2 pointers in
the queue that
keep track of the data
while performing
operations on
queue REAR and
FRONT.
The Queue ADT
• The Queue stores arbitrary • Auxiliary queue operations:
objects – front(): returns the element at
• Insertions and deletions follow the front without removing it
the first-in first-out (FIFO) – size(): returns the number of
scheme elements stored
• Insertions are at the rear of the – isEmpty(): returns a Boolean
queue and removals are at the value indicating whether no
front of the queue elements are stored
• Main queue operations:
– enqueue(object o): inserts
• Exceptions
element o at the end of the – Attempting the execution of
queue dequeue or front on an empty
– dequeue(): removes and returns queue throws an
the element at the front of the EmptyQueueException
queue
14
Applications of Queues
• Direct applications
– Waiting lines
– Access to shared resources (e.g., printer)
• Indirect applications
– Auxiliary data structure for algorithms
– Round Robin Scheduling.
– Keyboard Buffer.
15
OPERATIONS ON QUEUE
• enqueue():- Add or store item on queue. The element
is always enqueued in a queue from rear end.
• dequeue():- remove or access an element from
queue. The element is always dequeued from a
queue from the front end.
• peek():- Gets the element at the front of the queue
without removing it.
• isfull() − Checks if the queue is full.
• isempty() − Checks if the queue is empty.
Enqueue Operation
•ALGORITHM
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Dequeue Operation
• ALGORITHM:-
procedure dequeue
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
OVERFLOW AND UNDERFLOW
• Overflow results from • Underflow happens
trying to add an when trying to remove
element onto a an element from an
full queue empty queue.
• To avoid this check • To avoid this
isfull():- check isEmpty():-
Performance and Limitations
- array-based implementation of queue
• Performance
– Let n be the number of elements in the queue
– The space used is O(n)
– Each operation runs in time O(1)
• Limitations
– The maximum size of the queue must be defined a
priori , and cannot be changed
– Trying to enqueue an element into a full queue causes
an implementation-specific exception
Queue Summary
• Queue Operation Complexity for Different
Array
Implementations Array List
Fixed-Size Expandable (doubling Singly-
strategy) Linked
dequeue() O(1) O(1) O(1)
7/19/21 04:38 AM
Vectors 21
Circular Queue
Deque (Double
ended queue)
CIRCULAR QUEUE
CIRCULAR QUEUE Circular Queue is a
linear data structure in The last position is
which the operations connected back to the
are performed based on first position to make a
FIFO (First In First Out) circle.
principle .
In a normal Queue, we
can insert elements until
queue becomes full. But
once queue becomes
It is also called ‘Ring full, we can not insert
Buffer’. the next element even if
there is a space in front
of queue.
OPERATIONS ON CIRCULAR
QUEUE
• INSERTION ALGORITHM:- • DELETION ALGORITHM:-
• As we have checked
for the priority while
inserting itself
• Hence, while dequeue
we need not check the
priorites.
• Simply remove the
element from front as
in normal with the
linear queue.
Dijkstra's algorithm.
Huffman coding.
Applications The Real-time Optimally Adapting
Meshes (ROAM) algorithm.
of Priority Prim's algorithm for minimum
Queue spanning tree
DEQUE (DOUBLE ENDED QUEUE)
7/19/21 04:39 AM
Vectors 44
THANK YOU