0% found this document useful (0 votes)
184 views45 pages

Data Structures (Queue) : Madhuri Kalani

This document discusses different data structures used to store data, including queues. It defines a queue as a linear data structure that follows the FIFO (first in, first out) principle. Elements are inserted at the rear and deleted from the front. The document also discusses common queue operations like enqueue, dequeue, and peek. It provides examples of applications of queues and discusses different types of queues like circular queues and priority queues.

Uploaded by

Madhuri Kalani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
184 views45 pages

Data Structures (Queue) : Madhuri Kalani

This document discusses different data structures used to store data, including queues. It defines a queue as a linear data structure that follows the FIFO (first in, first out) principle. Elements are inserted at the rear and deleted from the front. The document also discusses common queue operations like enqueue, dequeue, and peek. It provides examples of applications of queues and discusses different types of queues like circular queues and priority queues.

Uploaded by

Madhuri Kalani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

DATA STRUCTURES

(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

Data • An ADT specifies:


• Data stored
Types • Operations on the data
(ADTs) • Error conditions associated with
operations

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)

enqueue(o) O(1) O(n) Worst Case O(1)


O(1) Best Case
O(1) Average Case
front() O(1) O(1) O(1)

Size(), isEmpty() O(1) O(1) O(1)

7/19/21 04:38 AM
Vectors 21
Circular Queue

TYPES OF QUEUE Priority 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:-

 Step 1: IF (REAR+1)%MAX = FRONT  Step 1: IF FRONT = -1


Write " OVERFLOW " Write " UNDERFLOW "
Goto step 4 Goto Step 4
[End OF IF] [END of IF]
 Step 2: SET VAL = QUEUE[FRONT]
 Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0  Step 3: IF FRONT = REAR
ELSE IF REAR = MAX - 1 and FRONT ! SET FRONT = REAR = -1
ELSE
=0
IF FRONT = MAX -1
SET REAR = 0 SET FRONT = 0
ELSE ELSE
SET REAR = (REAR + 1) % MAX SET FRONT = FRONT + 1
[END OF IF] [END of IF]
 Step 3: SET QUEUE[REAR] = VAL [END OF IF]
 Step 4: EXIT  Step 4: EXIT
PRIORITY QUEUE 

• In priority queue each element


is assigned a priority.
• The priority determines the
order in which the elements
will be processed.
• The element with higher
priority will be processed first 
• If two elements having
same priority are encountered
in a queue then they will be
processed on the first come first
server basis.
• Hence the rule of priority
comes into picture first and
once it turns ambigous the
queue principle is used
ENQUEUE IN PRIORITY QUEUE

•Rules for insertion:-

•1)Check priority with the


element at rear
•2)If the new element has
greater priority the dequeue
the element at rear and
insert the new element and
then the one which was
dequeued.
•3)Otherwise enqueue
normally.
DEQUEUE IN PRIORITY QUEUE

• 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)

• DeQue stands for


Double Ended
Queue. 
• Tt is just like a queue
but does not support
FIFO structure. 
• Insertion and
deletion can be done
from both
side( FRONT & REAR).
The Double-Ended Queue ADT
• The Double-Ended Queue, or • Auxiliary queue operations:
Deque, ADT stores arbitrary – first(): returns the element at
objects.  the front without removing it
• Richer than stack or queue ADTs. – last(): returns the element at
Supports insertions and deletions at the front without removing it
both the front and the end.
– size(): returns the number of
• Main deque operations: elements stored
– insertFirst(object o): inserts – isEmpty(): returns a Boolean
element o at the beginning of value indicating whether no
the deque elements are stored
– insertLast(object o): inserts • Exceptions
element o at the end of the
deque – Attempting the execution of
dequeue or front on an empty
– RemoveFirst(): removes and
queue throws an
returns the element at the front
EmptyDequeException
of the queue
– RemoveLast(): removes and
returns the element at the end
of the queue
39
Insertion at front end
Insertion at rear end
Deletion from front end
Deletion from rear end
Deque Summary
• Deque Operation Complexity for Different
Array Array
Implementations List List
Fixed- Expandable Singly- Doubly-
Size (doubling strategy) Linked Linked
removeFirst(), O(1) O(1) O(n) for O(1)
removeLast() one at list
tail, O(1) for
other
insertFirst(o), O(1) O(n) Worst Case O(1) O(1)
InsertLast(o) O(1) Best Case
O(1) Average Case
first(), last O(1) O(1) O(1) O(1)
Size(), O(1) O(1) O(1) O(1)
isEmpty()

7/19/21 04:39 AM
Vectors 44
THANK  YOU 

You might also like