SlideShare a Scribd company logo
1 of 32
Download to read offline
Queue            www.eshikshak.co.in

Data Structure
Introduction
● It is linear data structure
● It is collection of items – List
● Queue means line of items waiting for
  their turn
● Queue has two ends
  ○ Elements are added at one end.
  ○ Elements are removed from the other end.


              www.eshikshak.co.in
Queue
● Removal of data item is restricted at one
  end known as FRONT
● Insertion of data item is restricted at other
  end known as REAR
● The FRONT and REAR are used in
  describing a linear list, when queue is
  implemented
● First element inserted in list, will be the
  first to be removed - FIFO
               www.eshikshak.co.in
Queue as FIFO
● The element inserted first will be removed
  first from the Queue
● Thus, Queue is known as FIFO (First In-
  First Out) or FCFS (First Come First
  Serve)

● Examples of Queue
  ○ People waiting in Queue to purchase tickets at
    railway station or cinema hall, where the first person
    in the queue will be served first
                    www.eshikshak.co.in
Representation of Queue
● It has two pointer varaibles
  ○ FRONT : Containing the location of the front
    element of the queue
  ○ REAR : Containing the location of the rear
    element of the queue
● When queue is empty
    FRONT = -1 and REAR = -1



               www.eshikshak.co.in
Operations of Queue
● Insertion
  ○ Adding an element in queue will increased
    value of REAR by 1
     ■ REAR = REAR + 1
● Removal
  ○ Removing an element from queue will
    increased value of FRONT by 1
     ■ FRONT = FRONT + 1



              www.eshikshak.co.in
Queue       FRONT = -1, REAR =
Empty       -1
ADD(Q,10)   FRONT = 0 ,REAR =
            0
ADD(Q,20)   FRONT = 0 ,REAR
            =1
ADD(Q,30)   FRONT = 0 ,REAR
            =2
ADD(Q,40)   FRONT = 0 ,REAR
            =3
ADD(Q,50) FRONT = 0 ,REAR
          =4
Remove(Q) FRONT = 1 ,REAR
          =4
Remove(Q) FRONT = 2 ,REAR
          =4
Remove(Q) FRONT = 3 ,REAR
           =4
ADD(Q,     FRONT = 3 ,REAR
60)        =1         www.eshikshak.co.in
Types of Queue
● Queue as Array
● Circular Queue
● Priority Queue
● Input Restricted Queue
● Output Restricted Queue
● Dqueue



             www.eshikshak.co.in
Queue as Array : Insert
Initially when the QUEUE is empty, set FRONT = NULL and REAR = 0


Step 1: start
Step2: [check for queue is over flow or not]
If (REAR >n) or (REAR==FRONT)
Print “queue is overflow”
else
go to step 3
Step 3: [enter the item]
QUEUE[REAR]=value
REAR=REAR+1
Step 4:[ check condition]
If(FRONT==null)
FRONT=0
Step 5:end
Queue as Array : Delete

Step 1: start
Step 2: [check for queue is under flow or not]
If front>N or front==Null
Print ”queue is underflow”
else
goto step 3
Step 3: [check condition]
If front==rear
Front==null
Rear=0
else
goto step 4
Step 4: [delete element]
Queue[front]=null
Step 5: front=front+1
Step 6: end
Circular Queue
● To solve this problem, queues implement
  wrapping around. Such queues are called
  Circular Queues.
● Both the front and the rear pointers wrap
  around to the beginning of the array.
● It is also called as “Ring buffer”.
● Items can inserted and deleted from a queue in
  O(1) time.



                www.eshikshak.co.in
Circular Queue
● When a new item is inserted at the rear, the
  pointer to rear moves upwards.
● Similarly, when an item is deleted from the
  queue the front arrow moves downwards.
● After a few insert and delete operations the rear
  might reach the end of the queue and no more
  items can be inserted although the items from
  the front of the queue have been deleted and
  there is space in the queue.


                www.eshikshak.co.in
QINSERT(Queue, N, FRONT, REAR, ITEM)
1 [Queue already filled ?]
if FRONT = 0 and REAR = N-1, or if FRONT=REAR + 1 then
write : Overflow and Return
2 [Find new value of REAR]
if FRONT=-1 then [Queue initially empty]
Set FRON T = 0 and REAR = 0
else if REAR = N-1 then
Set REAR = 1
else
Set REAR = REAR + 1
[End of If structure]
3 Set QUEUE[REAR] = ITEM [This inserts new element]
4 Return
QDELETE(Queue, N, FRONT, REAR,
                ITEM)
1 [Queue already empty]
If FRONT = -1, then Write : Underflow and Return
2 Set ITEM = Queue[FRONT]
3 [Find new value of FRONT]
If FRONT = REAR, then [Queue has only one element to start]
Set FRONT = -1 and REAR = -1
Else if FRONT = N-1, then
Set FRONT = 0
Else
Set FRONT = FRONT + 1
[End of If Structure]
4 Return
Priority Queue




 www.eshikshak.co.in
Priority Queue
● It is collection of elements where elements are
  stored according to the their priority levels
● Inserting and removing of elements from queue
  is decided by the priority of the elements
● The two fundamental methods of a priority
  queue P:
   ○ insertItem(k,e): Insert an element e with key k into P.
   ○ removeMin(): Return and remove from P an element
     with the smallest key.




                   www.eshikshak.co.in
Priority Queue
● When you want to determine the priority
  for your assignments, you need a value
  for each assignment, that you can
  compare with each other.

● key: An object that is assigned to an
  element as a specific attribute for that
  element, which can be used to identify,
  rank, or weight that element.

               www.eshikshak.co.in
Example: Student records
Any of the attributes, Student Name, Student Number, or Final Score
         Student Name       ID              Final Score (out
can be used as keys.                        of 450)
Note: Keys may not be unique (Final Score).
        Ashwin            09BCA08          310
        Payal             09BCA80          311
        Darshika          09BCA24          380
        Nilkamal          09BCA75          400
        Nikunj            09BCA74          440
        Mori              09BCA102         400




                       www.eshikshak.co.in
Rules to maintain a Priority Queue
● The elements with the higher priority will
  be processed before any element of lower
  priority
● If there are elements with the same
  priority, then the element added first in
  the queue would get processed




              www.eshikshak.co.in
Priority Queue Insert
PQInsert (M, Item)
Step 1 Find the Row M
Step2 [Reset the Rear Pointer]
If Rear[M] = N-1 then Rear[M] = 0
Else
Rear[M] = Rear[M]+1
Step 3 [Overflow]
If Front[M] = Rear[M] then Write (“This Priority Queue is full”)
Return
Step 4 [Insert Element]
Q[M] [Rear[M]] = then
Step 5 [Is Front Pointer Properly Set]
If Front[M] = -1 then Front[m] = 0
Return
Step 6 Exit



                            www.eshikshak.co.in
Priority Queue Delete
PQDelete (K, Item)
Step 1 Initialize K = 0
Step 2 while (Front[K] = -1)
K = K+1
[To find the first non empty queue]
Step 3 [Delete Element]
Item = Q[K] [Front[K]
Step 4 [Queue Empty]
If Front[K] = N-1 then Front[K] = 0
Else
Front[K] = Front[K]+1
Return Item
Step 6 Exit



                         www.eshikshak.co.in
Deque
● Deque stands for double-end queue

● A data structure in which elements can be
  added or deleted at either the front or rear

● But no changes can be made in the list

● Deque is generalization of both stack and
  queue

                 www.eshikshak.co.in
Removing Element from Deque
● There are two variations of a deque.
  These are
  ○ Input Restricted Deque
     ■ An input restricted deque restricts the insertion of
       elements at one end only, but the deletion of
       elements can perform at both the ends.

  ○ Output Restricted Deque
     ■ An output restricted queue, restricts the deletion
       of elements at one end only, and allows insertion
       to be done at both the ends of deque

                 www.eshikshak.co.in
Possibilities
● The two possibilities that must be
  considered while inserting or deleting
  elements into the queue are :
  ○ When an attempt is made to insert an
    element into a deque which is already full, an
    overflow occurs.
  ○ When an attempt is made to delete an
    element from a deque which is empty,
    underflow occurs.



               www.eshikshak.co.in
Representation of Deque


Deletio                                           Insertion
n
Insertion                                         Deletio
                                                  n


              Front                         Rea
                                            r




                      www.eshikshak.co.in
Inserting Element in Deque
 InsertQatEnd(dq,7)            7



                       Front        Rear



InsertQatBeg(dq,10)            7



                      Front        Rear




  InsertQatBeg(dq,             7
  10)

                      Front        Rear
                                   www.eshikshak.co.in
InsertQatBeg(dq,10)    10            7


                      Front        Rear

InsertQatEnd(dq,4)
                       10            7         4


                      Front                   Rear


InsertQatBeg(dq,15)    15            10        7      4


                      Front                          Rear

InsertQatEnd(dq,55)
                       15            10        7      4     55


                      Front   www.eshikshak.co.in           Rear
Removing Element from Deque
delQBeg()                  10          7     4    55


                           Front                  Rear


delQBeg()                              7     4    55


                                     Front        Rear


InsertQatEnd(dq,23)          7          4    55    23


                             Front                Rear




                      www.eshikshak.co.in
Deque : Insert
● There are two variations of a deque.
  These are
  ○ Input Restricted Deque
     ■ An input restricted deque restricts the insertion of
       elements at one end only, but the deletion of
       elements can perform at both the ends.

  ○ Output Restricted Deque
     ■ An output restricted queue, restricts the deletion
       of elements at one end only, and allows insertion
       to be done at both the ends of deque

                 www.eshikshak.co.in
Input Restricted queue : Insert Element
Step 1: start
Step 2:[check condition for overflow]
If(rear==N-1 && front==0 or front=rear+1)
Print “over flow”
else
goto step 3
Step 3: [check condition]
If(front==null)
front = 0 and rear=-1
else
goto step 4
Step 4: [check condition and value]
If (rear== N -1)
rear=0
Else
rear=rear+1
Step 5: [Add value]
dq[rear]=value
Step 6:end


                              www.eshikshak.co.in
Input Restricted queue : Delete Beginning
Step 1: start
Step 2: [check condition for underflow]
If(rear==null & front==null)
Print”underflow”
else
goto step 3
Step 3: [delete]
dq[rear]=null
Step 4: [check condition]
If(rear==front)
front=rear=null
else
rear--;
Step 5: end



                         www.eshikshak.co.in
Input Restricted queue : Delete End
Step 1: start
Step2 : [check condition for under flow)
If(rear==null & front==null)
Print “under flow”
else
goto step 3
Step 3: [delete]
dq[front]=null
Step 4: [check condition]
If(rear==front)
rear=-1 and front=null
else
front=front+1
Step 5: end



                         www.eshikshak.co.in

More Related Content

What's hot (20)

Linked List
Linked ListLinked List
Linked List
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queues
QueuesQueues
Queues
 
Stack project
Stack projectStack project
Stack project
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
single linked list
single linked listsingle linked list
single linked list
 
Stack
StackStack
Stack
 
Queues
Queues Queues
Queues
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Unit 4 external sorting
Unit 4   external sortingUnit 4   external sorting
Unit 4 external sorting
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Stacks
StacksStacks
Stacks
 

Viewers also liked (20)

Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queue
QueueQueue
Queue
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Queue
QueueQueue
Queue
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Circular queues
Circular queuesCircular queues
Circular queues
 
deque and it applications
deque and it applicationsdeque and it applications
deque and it applications
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 

Similar to Queue as data_structure (20)

Queue
QueueQueue
Queue
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Queue
QueueQueue
Queue
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Detalied information of queue
Detalied information of queueDetalied information of queue
Detalied information of queue
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Queue
QueueQueue
Queue
 
Queue data structures and operation on data structures
Queue data structures and operation on data structuresQueue data structures and operation on data structures
Queue data structures and operation on data structures
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
 
Dynamic Queue.pptx
Dynamic Queue.pptxDynamic Queue.pptx
Dynamic Queue.pptx
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
QUEUES
QUEUESQUEUES
QUEUES
 
2.1 Queue.pptx
2.1 Queue.pptx2.1 Queue.pptx
2.1 Queue.pptx
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 

More from eShikshak

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluationeShikshak
 
Operators in python
Operators in pythonOperators in python
Operators in pythoneShikshak
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in pythoneShikshak
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythoneShikshak
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerceeShikshak
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computingeShikshak
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computingeShikshak
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloudeShikshak
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computingeShikshak
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computingeShikshak
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'eShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'eShikshak
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssionseShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variableseShikshak
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorseShikshak
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppteShikshak
 

More from eShikshak (20)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 

Recently uploaded

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsManeerUddin
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Recently uploaded (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Food processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture honsFood processing presentation for bsc agriculture hons
Food processing presentation for bsc agriculture hons
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

Queue as data_structure

  • 1. Queue www.eshikshak.co.in Data Structure
  • 2. Introduction ● It is linear data structure ● It is collection of items – List ● Queue means line of items waiting for their turn ● Queue has two ends ○ Elements are added at one end. ○ Elements are removed from the other end. www.eshikshak.co.in
  • 3. Queue ● Removal of data item is restricted at one end known as FRONT ● Insertion of data item is restricted at other end known as REAR ● The FRONT and REAR are used in describing a linear list, when queue is implemented ● First element inserted in list, will be the first to be removed - FIFO www.eshikshak.co.in
  • 4. Queue as FIFO ● The element inserted first will be removed first from the Queue ● Thus, Queue is known as FIFO (First In- First Out) or FCFS (First Come First Serve) ● Examples of Queue ○ People waiting in Queue to purchase tickets at railway station or cinema hall, where the first person in the queue will be served first www.eshikshak.co.in
  • 5. Representation of Queue ● It has two pointer varaibles ○ FRONT : Containing the location of the front element of the queue ○ REAR : Containing the location of the rear element of the queue ● When queue is empty FRONT = -1 and REAR = -1 www.eshikshak.co.in
  • 6. Operations of Queue ● Insertion ○ Adding an element in queue will increased value of REAR by 1 ■ REAR = REAR + 1 ● Removal ○ Removing an element from queue will increased value of FRONT by 1 ■ FRONT = FRONT + 1 www.eshikshak.co.in
  • 7. Queue FRONT = -1, REAR = Empty -1 ADD(Q,10) FRONT = 0 ,REAR = 0 ADD(Q,20) FRONT = 0 ,REAR =1 ADD(Q,30) FRONT = 0 ,REAR =2 ADD(Q,40) FRONT = 0 ,REAR =3 ADD(Q,50) FRONT = 0 ,REAR =4 Remove(Q) FRONT = 1 ,REAR =4 Remove(Q) FRONT = 2 ,REAR =4 Remove(Q) FRONT = 3 ,REAR =4 ADD(Q, FRONT = 3 ,REAR 60) =1 www.eshikshak.co.in
  • 8. Types of Queue ● Queue as Array ● Circular Queue ● Priority Queue ● Input Restricted Queue ● Output Restricted Queue ● Dqueue www.eshikshak.co.in
  • 9. Queue as Array : Insert Initially when the QUEUE is empty, set FRONT = NULL and REAR = 0 Step 1: start Step2: [check for queue is over flow or not] If (REAR >n) or (REAR==FRONT) Print “queue is overflow” else go to step 3 Step 3: [enter the item] QUEUE[REAR]=value REAR=REAR+1 Step 4:[ check condition] If(FRONT==null) FRONT=0 Step 5:end
  • 10. Queue as Array : Delete Step 1: start Step 2: [check for queue is under flow or not] If front>N or front==Null Print ”queue is underflow” else goto step 3 Step 3: [check condition] If front==rear Front==null Rear=0 else goto step 4 Step 4: [delete element] Queue[front]=null Step 5: front=front+1 Step 6: end
  • 11. Circular Queue ● To solve this problem, queues implement wrapping around. Such queues are called Circular Queues. ● Both the front and the rear pointers wrap around to the beginning of the array. ● It is also called as “Ring buffer”. ● Items can inserted and deleted from a queue in O(1) time. www.eshikshak.co.in
  • 12. Circular Queue ● When a new item is inserted at the rear, the pointer to rear moves upwards. ● Similarly, when an item is deleted from the queue the front arrow moves downwards. ● After a few insert and delete operations the rear might reach the end of the queue and no more items can be inserted although the items from the front of the queue have been deleted and there is space in the queue. www.eshikshak.co.in
  • 13. QINSERT(Queue, N, FRONT, REAR, ITEM) 1 [Queue already filled ?] if FRONT = 0 and REAR = N-1, or if FRONT=REAR + 1 then write : Overflow and Return 2 [Find new value of REAR] if FRONT=-1 then [Queue initially empty] Set FRON T = 0 and REAR = 0 else if REAR = N-1 then Set REAR = 1 else Set REAR = REAR + 1 [End of If structure] 3 Set QUEUE[REAR] = ITEM [This inserts new element] 4 Return
  • 14. QDELETE(Queue, N, FRONT, REAR, ITEM) 1 [Queue already empty] If FRONT = -1, then Write : Underflow and Return 2 Set ITEM = Queue[FRONT] 3 [Find new value of FRONT] If FRONT = REAR, then [Queue has only one element to start] Set FRONT = -1 and REAR = -1 Else if FRONT = N-1, then Set FRONT = 0 Else Set FRONT = FRONT + 1 [End of If Structure] 4 Return
  • 16. Priority Queue ● It is collection of elements where elements are stored according to the their priority levels ● Inserting and removing of elements from queue is decided by the priority of the elements ● The two fundamental methods of a priority queue P: ○ insertItem(k,e): Insert an element e with key k into P. ○ removeMin(): Return and remove from P an element with the smallest key. www.eshikshak.co.in
  • 17. Priority Queue ● When you want to determine the priority for your assignments, you need a value for each assignment, that you can compare with each other. ● key: An object that is assigned to an element as a specific attribute for that element, which can be used to identify, rank, or weight that element. www.eshikshak.co.in
  • 18. Example: Student records Any of the attributes, Student Name, Student Number, or Final Score Student Name ID Final Score (out can be used as keys. of 450) Note: Keys may not be unique (Final Score). Ashwin 09BCA08 310 Payal 09BCA80 311 Darshika 09BCA24 380 Nilkamal 09BCA75 400 Nikunj 09BCA74 440 Mori 09BCA102 400 www.eshikshak.co.in
  • 19. Rules to maintain a Priority Queue ● The elements with the higher priority will be processed before any element of lower priority ● If there are elements with the same priority, then the element added first in the queue would get processed www.eshikshak.co.in
  • 20. Priority Queue Insert PQInsert (M, Item) Step 1 Find the Row M Step2 [Reset the Rear Pointer] If Rear[M] = N-1 then Rear[M] = 0 Else Rear[M] = Rear[M]+1 Step 3 [Overflow] If Front[M] = Rear[M] then Write (“This Priority Queue is full”) Return Step 4 [Insert Element] Q[M] [Rear[M]] = then Step 5 [Is Front Pointer Properly Set] If Front[M] = -1 then Front[m] = 0 Return Step 6 Exit www.eshikshak.co.in
  • 21. Priority Queue Delete PQDelete (K, Item) Step 1 Initialize K = 0 Step 2 while (Front[K] = -1) K = K+1 [To find the first non empty queue] Step 3 [Delete Element] Item = Q[K] [Front[K] Step 4 [Queue Empty] If Front[K] = N-1 then Front[K] = 0 Else Front[K] = Front[K]+1 Return Item Step 6 Exit www.eshikshak.co.in
  • 22. Deque ● Deque stands for double-end queue ● A data structure in which elements can be added or deleted at either the front or rear ● But no changes can be made in the list ● Deque is generalization of both stack and queue www.eshikshak.co.in
  • 23. Removing Element from Deque ● There are two variations of a deque. These are ○ Input Restricted Deque ■ An input restricted deque restricts the insertion of elements at one end only, but the deletion of elements can perform at both the ends. ○ Output Restricted Deque ■ An output restricted queue, restricts the deletion of elements at one end only, and allows insertion to be done at both the ends of deque www.eshikshak.co.in
  • 24. Possibilities ● The two possibilities that must be considered while inserting or deleting elements into the queue are : ○ When an attempt is made to insert an element into a deque which is already full, an overflow occurs. ○ When an attempt is made to delete an element from a deque which is empty, underflow occurs. www.eshikshak.co.in
  • 25. Representation of Deque Deletio Insertion n Insertion Deletio n Front Rea r www.eshikshak.co.in
  • 26. Inserting Element in Deque InsertQatEnd(dq,7) 7 Front Rear InsertQatBeg(dq,10) 7 Front Rear InsertQatBeg(dq, 7 10) Front Rear www.eshikshak.co.in
  • 27. InsertQatBeg(dq,10) 10 7 Front Rear InsertQatEnd(dq,4) 10 7 4 Front Rear InsertQatBeg(dq,15) 15 10 7 4 Front Rear InsertQatEnd(dq,55) 15 10 7 4 55 Front www.eshikshak.co.in Rear
  • 28. Removing Element from Deque delQBeg() 10 7 4 55 Front Rear delQBeg() 7 4 55 Front Rear InsertQatEnd(dq,23) 7 4 55 23 Front Rear www.eshikshak.co.in
  • 29. Deque : Insert ● There are two variations of a deque. These are ○ Input Restricted Deque ■ An input restricted deque restricts the insertion of elements at one end only, but the deletion of elements can perform at both the ends. ○ Output Restricted Deque ■ An output restricted queue, restricts the deletion of elements at one end only, and allows insertion to be done at both the ends of deque www.eshikshak.co.in
  • 30. Input Restricted queue : Insert Element Step 1: start Step 2:[check condition for overflow] If(rear==N-1 && front==0 or front=rear+1) Print “over flow” else goto step 3 Step 3: [check condition] If(front==null) front = 0 and rear=-1 else goto step 4 Step 4: [check condition and value] If (rear== N -1) rear=0 Else rear=rear+1 Step 5: [Add value] dq[rear]=value Step 6:end www.eshikshak.co.in
  • 31. Input Restricted queue : Delete Beginning Step 1: start Step 2: [check condition for underflow] If(rear==null & front==null) Print”underflow” else goto step 3 Step 3: [delete] dq[rear]=null Step 4: [check condition] If(rear==front) front=rear=null else rear--; Step 5: end www.eshikshak.co.in
  • 32. Input Restricted queue : Delete End Step 1: start Step2 : [check condition for under flow) If(rear==null & front==null) Print “under flow” else goto step 3 Step 3: [delete] dq[front]=null Step 4: [check condition] If(rear==front) rear=-1 and front=null else front=front+1 Step 5: end www.eshikshak.co.in