SlideShare a Scribd company logo
1 of 17
Definition of Queue
An ordered collection of items from which items may be deleted from one end called the front and into which items
may be inserted from other end called rear is known as Queue.
It is a linear data structure.
It is called First In First Out (FIFO) list. Since in queue, first element will be the first element out.
1
Deletion
Front Rear
Insertion
(a) Insertion and deletion of elements in Queue
Queue (Example)
2
Front Rear
(b) Queue
A B C D
Front Rear
A B C D E
(c) Front and near pointer after insertion
Front Rear
(d) Front and rear pointer after deletion
B C D E
Element E will be added at the rear end.Queue with four elements A, B, C and D.
A is at front and D is at rear.
Element can be deleted only from the front.
Thus, A will be the first to be removed from the queue.
Difference between Stack & Queue.
3
SR Stack Queue
1 Stack is a LIFO (Last in first out)
data structure.
Queue is a FIFO (first in first out)
data structure.
2 In a stack, all insertion and
deletions are permitted only at one
end of stack called top of stack.
In a queue items are inserted at
one end called the rear and
deleted from the other end called
the front.
3 Stack is widely used in recursion. Queue is more useful in many of
the real life applications.
4 Example: A stack of plates, a stack
of coin etc.
Example: A queue of people
waiting to purchase tickets, in a
computer system process waiting
for line printer etc.
Operations on a Queue.
1. Create: Creates empty queue.
2. Insert: Add new element at rear.
3. Delete: Delete element at front.
4. Isempty: Checks queue is empty or not.
5. Isfull: Checks queue is full or not.
4
Algorithm to insert and delete element in the Queue.
1. Create an empty queue by giving name and
2. Initially assign Rear = -1, front = -1.
3. If choice == Insert then
if Rear == max-1 then
print “queue is full”
else
Rear = Rear +1
q [Rear] = element
4. If choice == Delete then
If front == -1 then
print “queue is empty”
else
front = front +1
5. Stop
5
Queue as an abstract data type
(1) Initialize the queue to be empty.
(2) Check if the queue is empty or not.
(3) Check if the queue is full or not.
(4) Insert a new element in the queue if it is not full.
(5) Retrieve the value of first element of the queue, if the queue is not empty.
(6) Delete the first element from the queue if it is not empty.
Queue abstract data type = Queue elements + Queue operations.
6
Representation of an Queue as an Array
7
Deletion
Front Rear
Deletion
(a) Queue with array
Front = -1 Rear = -1
0 1 2 3 4
0 1 2 3 4 0 1 2 3 4
0 1 2 3 4
(b) Empty Queue
(c) Queue after adding an element U (d) Queue after adding 5 element
Front = 0
Rear = 0
Front = 0 Rear = 4
Front = 1
U V X Y ZU
V W X Y
Rear = 4
(E) Queue after adding U element
Types of Queue
1. Linear Queue.
2. Circular Queue.
3. Double ended Queue.
4. Priority Queue.
8
Circular Queue full (overflow)
9
60
30
60
50
40
20
10
Q[5]
Q[1]
Q[4]
Q[0]
Q[3] Q[2]
Circular queue full
10
Circular Queue Empty (underflow)
60
Q[5]
Q[1]
Q[4]
Q[0]
Q[3] Q[2]
Circular queue empty
Circular Queue Diagram & Algorithm (to insert)
11
Algorithm to insert an element into circular Queue.
Step 1: If front = 0 and rear = MAX -1 then
write ‘circular queue is overflow’
else if front = -1 and rear =-1 then set
front = rear = 0
else if rear = MAX -1 and front !=0
set rear = 0
else
set rear = rear +1
Step 2: Set queue[rear] = val
Step 3: Exit
60 60
Q[0]
Q[1]
Q[4]
Q[2]Q[3]
Q[4]
Q[3] Q[2]
Q[1]
Q[0]
Rear
Rear
Front Front
20
5
10
5
20
10
30
(a) (b)
• If a new element is to be inserted in the queue,
the position of the element to be inserted will be
calculated by the relation as follows:
Rear = (rear +1) % MAXSIZE
• If we add an element 30 to the queue. The rear is calculated as follows:
rear = (rear +1) % MAXSIZE
=(2+1) % 5
=3
Circular Queue Diagram & Algorithm (to delete)
12
Algorithm to delete an element into circular Queue.
Step 1: If front = -1 the write ‘queue underflow’
Step 2: Set val = queue[front]
Step 3: if front = rear
set front = rear = -1
else
if front = MAX – 1
set front = 0
else
set front = front = -1
Step 4: Exit
60
Q[0]
Q[1]
Q[2]Q[3]
Q[4]
Rear
Front
20
5
10
(c)
The front is calculated as follow:
front = (0 + 1) % 5 = 1
4
Double ended Queue & Type
13
A deque is a list in which the elements can be inserted or deleted at either end.
In a dequeue two pointers are maintained LEFT and RIGHT which point to either end of the dequeue.
Front Rear
Insertion
Deletion Insertion
Deletion
Left = 3 Right = 7
0 1 2 3 4 5 6 7 8 9
29 37 45 54 63
(a) Double-ended queue
(1) Input Restricted dequeue: In this dequeue, insertion can be done only at one of the dequeue while deletions can
be done from both ends.
Insertion
Deletion
Deletion
(b) Input restricted dequeue
Double Ended Queue Algorithm to Insert at the Rear & Front End
14
Algorithm to insert at the Rear End.
Step 1: if (Rear = (max -1)) then
print “Queue is full”
Step 2: set Rear = Rear +1
Step 3: Queue[Rear] = element
Step 4: Exit
Algorithm to insert at the Front End.
Step 1: if front == 0 then
print “Queue is full”
rerun
Step 2: front = front -1
Step 3: Queue[front] = element
Step 4: Exit
Double Ended Queue Algorithm to Delete from the Rear & Front End
15
Algorithm to delete at the Rear End.
Step 1: if (front == rear) then
print “Queue is empty”
return
Step 2: Rear = Rear -1
Step 3: element = Queue[Rear]
Step 4: Exit
Algorithm to insert at the Front End.
Step 1: if (front == Rear) then
print “Queue is empty”
rerun
Step 2: front = front +1
Step 3: element = Queue[front]
Step 4: Exit
Priority Queue
In a priority queue ( i ) each element is assign a priority ( ii ) The elements are processed according to
• Highest priority element is processed before lower priority element.
• Two elements of same priority are processed according to the order of insertion.
A priority queue contains a collection of items that are waiting to be processed. In queue, items are removed for
processing in the same order in which they are added to the queue.
In a priority queue, however, each item, has a priority, and when its time to select an item, the item with the highest
priority is the one that is order.
16
Types of Priority Queue
1. Ascending Priority Queue
2. Descending Priority Queue
Application of Queue
• Queue are used in computer for scheduling of resources to application . These
resources are CPU, Printer etc.
• In batch Programming, multiple jobs are combined into a batch and the first program is
executed first, the second is executed next and so on in a sequential manners.
• A queue is used in break first search traversal of a graph & level wise traversal of tree.
• Computer simulation, Airport simulation.
17

More Related Content

What's hot

Dfs presentation
Dfs presentationDfs presentation
Dfs presentationAlizay Khan
 
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 structureVardhil Patel
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturergomathi chlm
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)Huba Akhtar
 
Queue data structure
Queue data structureQueue data structure
Queue data structureanooppjoseph
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
Linked list
Linked listLinked list
Linked listakshat360
 

What's hot (20)

Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
 
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
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Stacks and Queue - Data Structures
Stacks and Queue - Data StructuresStacks and Queue - Data Structures
Stacks and Queue - Data Structures
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Linked List
Linked ListLinked List
Linked List
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
single linked list
single linked listsingle linked list
single linked list
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Double Linked List (Algorithm)
Double Linked List (Algorithm)Double Linked List (Algorithm)
Double Linked List (Algorithm)
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Linked list
Linked listLinked list
Linked list
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Queue
QueueQueue
Queue
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Linked list
Linked listLinked list
Linked list
 

Similar to Queue - Data Structure - Notes

Similar to Queue - Data Structure - Notes (20)

Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queue
QueueQueue
Queue
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
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)
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
05 queues
05 queues05 queues
05 queues
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Queue
QueueQueue
Queue
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Lect 17-18 Zaheer Abbas
Lect 17-18 Zaheer AbbasLect 17-18 Zaheer Abbas
Lect 17-18 Zaheer Abbas
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 

More from Omprakash Chauhan

Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesOmprakash Chauhan
 
Basic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesBasic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesOmprakash Chauhan
 
Polygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesPolygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesOmprakash Chauhan
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesOmprakash Chauhan
 
Basic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesBasic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesOmprakash Chauhan
 
E-R Diagram of College Management Systems
E-R Diagram of College Management SystemsE-R Diagram of College Management Systems
E-R Diagram of College Management SystemsOmprakash Chauhan
 
Burglar Alarm Micro Project
Burglar Alarm Micro ProjectBurglar Alarm Micro Project
Burglar Alarm Micro ProjectOmprakash Chauhan
 
Simple Calculator Flowchart
Simple Calculator FlowchartSimple Calculator Flowchart
Simple Calculator FlowchartOmprakash Chauhan
 
Full Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectFull Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectOmprakash Chauhan
 
A detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsA detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsOmprakash Chauhan
 
Fractional-horsepower Motor Report
Fractional-horsepower Motor ReportFractional-horsepower Motor Report
Fractional-horsepower Motor ReportOmprakash Chauhan
 
motherboard electronic components and their functions
motherboard electronic components and their functionsmotherboard electronic components and their functions
motherboard electronic components and their functionsOmprakash Chauhan
 
How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.Omprakash Chauhan
 
Welcome to the world of ionization
Welcome to the world of ionization Welcome to the world of ionization
Welcome to the world of ionization Omprakash Chauhan
 

More from Omprakash Chauhan (19)

Introduction to curve
Introduction to curveIntroduction to curve
Introduction to curve
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Sorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - NotesSorting and Searching - Data Structure - Notes
Sorting and Searching - Data Structure - Notes
 
Basic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - NotesBasic of Data Structure - Data Structure - Notes
Basic of Data Structure - Data Structure - Notes
 
Polygons - Computer Graphics - Notes
Polygons - Computer Graphics - NotesPolygons - Computer Graphics - Notes
Polygons - Computer Graphics - Notes
 
Line Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - NotesLine Drawing Algorithms - Computer Graphics - Notes
Line Drawing Algorithms - Computer Graphics - Notes
 
Basic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - NotesBasic of computer graphic - Computer Graphic - Notes
Basic of computer graphic - Computer Graphic - Notes
 
E-R Diagram of College Management Systems
E-R Diagram of College Management SystemsE-R Diagram of College Management Systems
E-R Diagram of College Management Systems
 
Burglar Alarm Micro Project
Burglar Alarm Micro ProjectBurglar Alarm Micro Project
Burglar Alarm Micro Project
 
Simple Calculator Flowchart
Simple Calculator FlowchartSimple Calculator Flowchart
Simple Calculator Flowchart
 
Full Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) MicroprojectFull Wave Rectifier (FWR) Microproject
Full Wave Rectifier (FWR) Microproject
 
A detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skillsA detailed study of guidelines required for presentation skills
A detailed study of guidelines required for presentation skills
 
Fractional-horsepower Motor Report
Fractional-horsepower Motor ReportFractional-horsepower Motor Report
Fractional-horsepower Motor Report
 
motherboard electronic components and their functions
motherboard electronic components and their functionsmotherboard electronic components and their functions
motherboard electronic components and their functions
 
How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.How To Area of irregular shape Using Integration Method.
How To Area of irregular shape Using Integration Method.
 
Process of ionization
Process of ionizationProcess of ionization
Process of ionization
 
Welcome to the world of ionization
Welcome to the world of ionization Welcome to the world of ionization
Welcome to the world of ionization
 
Printer
PrinterPrinter
Printer
 
System of units
System of unitsSystem of units
System of units
 

Recently uploaded

System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxachiever3003
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxNiranjanYadav41
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentBharaniDharan195623
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 

Recently uploaded (20)

System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptx
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptx
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managament
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 

Queue - Data Structure - Notes

  • 1. Definition of Queue An ordered collection of items from which items may be deleted from one end called the front and into which items may be inserted from other end called rear is known as Queue. It is a linear data structure. It is called First In First Out (FIFO) list. Since in queue, first element will be the first element out. 1 Deletion Front Rear Insertion (a) Insertion and deletion of elements in Queue
  • 2. Queue (Example) 2 Front Rear (b) Queue A B C D Front Rear A B C D E (c) Front and near pointer after insertion Front Rear (d) Front and rear pointer after deletion B C D E Element E will be added at the rear end.Queue with four elements A, B, C and D. A is at front and D is at rear. Element can be deleted only from the front. Thus, A will be the first to be removed from the queue.
  • 3. Difference between Stack & Queue. 3 SR Stack Queue 1 Stack is a LIFO (Last in first out) data structure. Queue is a FIFO (first in first out) data structure. 2 In a stack, all insertion and deletions are permitted only at one end of stack called top of stack. In a queue items are inserted at one end called the rear and deleted from the other end called the front. 3 Stack is widely used in recursion. Queue is more useful in many of the real life applications. 4 Example: A stack of plates, a stack of coin etc. Example: A queue of people waiting to purchase tickets, in a computer system process waiting for line printer etc.
  • 4. Operations on a Queue. 1. Create: Creates empty queue. 2. Insert: Add new element at rear. 3. Delete: Delete element at front. 4. Isempty: Checks queue is empty or not. 5. Isfull: Checks queue is full or not. 4
  • 5. Algorithm to insert and delete element in the Queue. 1. Create an empty queue by giving name and 2. Initially assign Rear = -1, front = -1. 3. If choice == Insert then if Rear == max-1 then print “queue is full” else Rear = Rear +1 q [Rear] = element 4. If choice == Delete then If front == -1 then print “queue is empty” else front = front +1 5. Stop 5
  • 6. Queue as an abstract data type (1) Initialize the queue to be empty. (2) Check if the queue is empty or not. (3) Check if the queue is full or not. (4) Insert a new element in the queue if it is not full. (5) Retrieve the value of first element of the queue, if the queue is not empty. (6) Delete the first element from the queue if it is not empty. Queue abstract data type = Queue elements + Queue operations. 6
  • 7. Representation of an Queue as an Array 7 Deletion Front Rear Deletion (a) Queue with array Front = -1 Rear = -1 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 (b) Empty Queue (c) Queue after adding an element U (d) Queue after adding 5 element Front = 0 Rear = 0 Front = 0 Rear = 4 Front = 1 U V X Y ZU V W X Y Rear = 4 (E) Queue after adding U element
  • 8. Types of Queue 1. Linear Queue. 2. Circular Queue. 3. Double ended Queue. 4. Priority Queue. 8
  • 9. Circular Queue full (overflow) 9 60 30 60 50 40 20 10 Q[5] Q[1] Q[4] Q[0] Q[3] Q[2] Circular queue full
  • 10. 10 Circular Queue Empty (underflow) 60 Q[5] Q[1] Q[4] Q[0] Q[3] Q[2] Circular queue empty
  • 11. Circular Queue Diagram & Algorithm (to insert) 11 Algorithm to insert an element into circular Queue. Step 1: If front = 0 and rear = MAX -1 then write ‘circular queue is overflow’ else if front = -1 and rear =-1 then set front = rear = 0 else if rear = MAX -1 and front !=0 set rear = 0 else set rear = rear +1 Step 2: Set queue[rear] = val Step 3: Exit 60 60 Q[0] Q[1] Q[4] Q[2]Q[3] Q[4] Q[3] Q[2] Q[1] Q[0] Rear Rear Front Front 20 5 10 5 20 10 30 (a) (b) • If a new element is to be inserted in the queue, the position of the element to be inserted will be calculated by the relation as follows: Rear = (rear +1) % MAXSIZE • If we add an element 30 to the queue. The rear is calculated as follows: rear = (rear +1) % MAXSIZE =(2+1) % 5 =3
  • 12. Circular Queue Diagram & Algorithm (to delete) 12 Algorithm to delete an element into circular Queue. Step 1: If front = -1 the write ‘queue underflow’ Step 2: Set val = queue[front] Step 3: if front = rear set front = rear = -1 else if front = MAX – 1 set front = 0 else set front = front = -1 Step 4: Exit 60 Q[0] Q[1] Q[2]Q[3] Q[4] Rear Front 20 5 10 (c) The front is calculated as follow: front = (0 + 1) % 5 = 1 4
  • 13. Double ended Queue & Type 13 A deque is a list in which the elements can be inserted or deleted at either end. In a dequeue two pointers are maintained LEFT and RIGHT which point to either end of the dequeue. Front Rear Insertion Deletion Insertion Deletion Left = 3 Right = 7 0 1 2 3 4 5 6 7 8 9 29 37 45 54 63 (a) Double-ended queue (1) Input Restricted dequeue: In this dequeue, insertion can be done only at one of the dequeue while deletions can be done from both ends. Insertion Deletion Deletion (b) Input restricted dequeue
  • 14. Double Ended Queue Algorithm to Insert at the Rear & Front End 14 Algorithm to insert at the Rear End. Step 1: if (Rear = (max -1)) then print “Queue is full” Step 2: set Rear = Rear +1 Step 3: Queue[Rear] = element Step 4: Exit Algorithm to insert at the Front End. Step 1: if front == 0 then print “Queue is full” rerun Step 2: front = front -1 Step 3: Queue[front] = element Step 4: Exit
  • 15. Double Ended Queue Algorithm to Delete from the Rear & Front End 15 Algorithm to delete at the Rear End. Step 1: if (front == rear) then print “Queue is empty” return Step 2: Rear = Rear -1 Step 3: element = Queue[Rear] Step 4: Exit Algorithm to insert at the Front End. Step 1: if (front == Rear) then print “Queue is empty” rerun Step 2: front = front +1 Step 3: element = Queue[front] Step 4: Exit
  • 16. Priority Queue In a priority queue ( i ) each element is assign a priority ( ii ) The elements are processed according to • Highest priority element is processed before lower priority element. • Two elements of same priority are processed according to the order of insertion. A priority queue contains a collection of items that are waiting to be processed. In queue, items are removed for processing in the same order in which they are added to the queue. In a priority queue, however, each item, has a priority, and when its time to select an item, the item with the highest priority is the one that is order. 16 Types of Priority Queue 1. Ascending Priority Queue 2. Descending Priority Queue
  • 17. Application of Queue • Queue are used in computer for scheduling of resources to application . These resources are CPU, Printer etc. • In batch Programming, multiple jobs are combined into a batch and the first program is executed first, the second is executed next and so on in a sequential manners. • A queue is used in break first search traversal of a graph & level wise traversal of tree. • Computer simulation, Airport simulation. 17