SlideShare a Scribd company logo
QUEUE
Data Structures
Introduction
 A Data Structure in which data can only be
inserted at one end (the rear), and deleted
from the other end (the front)
 These restrictions ensure that data is
processed through queue in the order in which
they are received (FIFO).
Usage Scenarios
Bus Stop Queue
 Remove a person from the queue
Bus
Stop
front
rear
rear rear rear rear
Bus Stop Queue
Bus
Stop
front
rear
rear rear
Bus Stop Queue
Bus
Stop
front
rear
rear
Bus Stop Queue
 Add a person to the queue
 A queue is a FIFO (First-In, First-Out) list.
Bus
Stop
front
rear
rear
A Print Queue
 Print queue: printing is much slower than the
process of selecting pages to print and so a
queue is used
The Unsuitability of a Print
Stack
 Stacks are last-in, first-out (LIFO)
 The most recently selected document would
be the next to print
 Unless the printer stack is empty, your print job
may never get executed if others are issuing
print jobs.
 
Program #1
Program #2front
rear
Send a message to the queue
Take a message from
the queue
Message queues in an operating
system
 There are times that programs need to
communicate with each other. Unix operating
system provides message queue as one of the
mechanisms to facilitate communication.
Example: Web spider
 Search engines (Altavista, Google, etc) uses
programs called spiders to discover new pages on the
web.
 Input = some particular seed page
 Repeatedly: select a previously-discovered
page, and traverse one of its hyperlinks.
To ensure broad coverage of the entire web,
spiders employ a queue data-structure
to store & select discovered pages
next page to
be explored
newly discovered
pages
Basic Queue Operations
 Enqueue: Insertion of an element at rear of queue
 Dequeue: deletion of an element at the front of
queue
 Queue front: examines the element at front of
queue
 Queue rear: examines element at rear of queue
Basic Queue Operations:
Underflow
Overflow
Exercise
 What would be the contents of queue Q after the
following code is executed and the following data are
entered?Q = createQueue
loop (not end of file)
read number
if (number not 0)
enqueue(Q, number)
else
dequeue(Q)
end if
end loop
Data: 
5,  7,  12,  4,  0,  4,  
8,  67,  23, 0,  44,  
33,  6,  0
Queue Implementation Issues
 Consider a queue implemented in an array
of size 10
 After 5 e nq ue ue operations, the shaded
portion of the array has been filled:
0 1 2 9
front rear
Queue Implementation Issues
 After two de q ue ue operations, if array
contents are not shifted, we have:
0 1 2 9
front rear
• The queue capacity is now reduced to 8
items
Queue Implementation Issues
 Option 1 using an underlying array: enhance the
e nq ue ue operation to increase the size of the
underlying array whenever there is an item in
the final array location:
 de q ue ue is a fast operation; e nq ue ue is fast except
when the array needs replacing
 Could use a large amount of space: once an item is
removed, its array location is never reused
Queue Implementation Issues
 Option 2
 Shift all the remaining items at each de q ue ue
operation
Circular Queues
 We can implement fixed-capacity queues
using arrays so that the queue capacity never
decreases: use circularqueues
 Concept can be extended so that the
underlying array can be replaced by a larger
array if necessary
Circular Queues
 Treat the underlying array as if it is circular:
in an n-element array, the location “before”
index 0 is index n-1; the location “after”
index n-1 is index 0
1
0
12
11
10
• Use modular arithmetic
to compute next and
previous indices
Circular Queues
1
0
12
11
10
1
0
12
11
10
1
0
12
11
10
After 7 enqueues
front
rear
After 5
dequeues
front
rear
After 8 more
enqueues
front
rear
Array Based Queue
Implementation
 template <class Qtype> class Queue {
 Qtype queue[SIZE];
 int front, rear;
 public:
 Queue() {
 front = rear= -1;
 }
 void enqueue(Qtype num);
 Qtype dequeue();
 bool isfull();
 Qtype isempty();
 };
isfull
 template <class Qtype> bool
Queue<Qtype>::isfull()
 {
 if(rear+1==front || (rear+1==SIZE && front==0)) {
 return true;
 }
 else{
 return false;
 }
 }
isempty
 template <class Qtype> bool
Queue<Qtype>::isempty()
 {
 if(rear==-1)
 return true;
 }
 else{
 return false;
 }
 }
enqueue
 template <class Qtype> void Queue<Qtype>::enqueue(Qtype num)
 {
 if(isfull()) {
 cout << "Queue is full.n";
 }
 else{
 if(front==-1){
 front++;
 }
 rear = (rear + 1) % SIZE;
 queue[rear] = item;
 }
 }
dequeue
 template <class Qtype> Qtype Queue<Qtype>::dequeue()
 {
 if(isempty()) {
 cout << "Queue is empty.n";
 return -999;
 }
 else{
 data=queue[front];
 if(front==rear){
 read=front=-1;
 }
 else{
 front = (front + 1) % SIZE;
 }
 return data;
 }
 }
Priority Queues
 Works like an ordinary queue.
 However, the order of how things are removed
depend on a priority key.
 Ascending Priority Queue – The item with the
smallest key has the highest priority.
 Descending Priority Queue – The item with the
largest key has the highest priority.
 They will be covered after later.
Queues vs. Stacks
 Stacks are a LIFO container => store data in the reverse of
order received
 Queues are a FIFO container => store data in the order
received
 Stacks then suggest applications where some sort of reversal
or unwinding is desired.
 From an ordering perspective, then, Queue are the “opposite”
of stacks
 Easy solution to the palindrome problem
End

More Related Content

What's hot

Sorting
SortingSorting
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
NalinNishant3
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Stack
StackStack
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
Ninad Mankar
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Queues
QueuesQueues
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
khabbab_h
 
Stack
StackStack
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Circular queue
Circular queueCircular queue
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar
 

What's hot (20)

Sorting
SortingSorting
Sorting
 
Heaps
HeapsHeaps
Heaps
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Linked list
Linked listLinked list
Linked list
 
Stack
StackStack
Stack
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Queues
QueuesQueues
Queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
 
Stack
StackStack
Stack
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Circular queue
Circular queueCircular queue
Circular queue
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 

Viewers also liked

Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
eShikshak
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
Queue
QueueQueue
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
Buxoo Abdullah
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
Queues
QueuesQueues
Queues
Hareem Aslam
 
Queues
QueuesQueues
Queues
Sadaf Ismail
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
mailmerk
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
Kuber Chandra
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
maamir farooq
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
Durga Devi
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queues
Andres Mendez-Vazquez
 
5.node js
5.node js5.node js
5.node js
Geunhyung Kim
 
салмина алина 1921_информатика
салмина алина 1921_информатикасалмина алина 1921_информатика
салмина алина 1921_информатика
alinochkavedenina
 

Viewers also liked (20)

Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Queue
QueueQueue
Queue
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queues
QueuesQueues
Queues
 
Queues
QueuesQueues
Queues
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Lec8
Lec8Lec8
Lec8
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queues
 
5.node js
5.node js5.node js
5.node js
 
by the numbers
by the numbersby the numbers
by the numbers
 
салмина алина 1921_информатика
салмина алина 1921_информатикасалмина алина 1921_информатика
салмина алина 1921_информатика
 

Similar to Queue in Data Structure

Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
Mekk Mhmd
 
Queue
QueueQueue
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
Tribhuvan University
 
basics of queues
basics of queuesbasics of queues
basics of queues
sirmanohar
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
Prof. Dr. K. Adisesha
 
2 b queues
2 b queues2 b queues
2 b queues
Nguync91368
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
SSE_AndyLi
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
Usha Mahalingam
 
stack.ppt
stack.pptstack.ppt
stack.ppt
ssuserec1395
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
singhprpg
 
Queue
QueueQueue
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
Intro C# Book
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
LavanyaJ28
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
cpjcollege
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
NuraMohamed9
 

Similar to Queue in Data Structure (20)

Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue
QueueQueue
Queue
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.pptCh03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
9 python data structure-2
9 python data structure-29 python data structure-2
9 python data structure-2
 
2 b queues
2 b queues2 b queues
2 b queues
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Queue
QueueQueue
Queue
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , 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
 

More from Muhazzab Chouhadry

Install guid to SQL server 2008
Install guid to SQL server 2008Install guid to SQL server 2008
Install guid to SQL server 2008
Muhazzab Chouhadry
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
Muhazzab Chouhadry
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
Muhazzab Chouhadry
 
Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
Muhazzab Chouhadry
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Sentence and its types
Sentence and its typesSentence and its types
Sentence and its types
Muhazzab Chouhadry
 

More from Muhazzab Chouhadry (6)

Install guid to SQL server 2008
Install guid to SQL server 2008Install guid to SQL server 2008
Install guid to SQL server 2008
 
Stack in Sata Structure
Stack in Sata StructureStack in Sata Structure
Stack in Sata Structure
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Data Structure Sorting
Data Structure SortingData Structure Sorting
Data Structure Sorting
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Sentence and its types
Sentence and its typesSentence and its types
Sentence and its types
 

Recently uploaded

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 

Queue in Data Structure

  • 2. Introduction  A Data Structure in which data can only be inserted at one end (the rear), and deleted from the other end (the front)  These restrictions ensure that data is processed through queue in the order in which they are received (FIFO).
  • 4. Bus Stop Queue  Remove a person from the queue Bus Stop front rear rear rear rear rear
  • 7. Bus Stop Queue  Add a person to the queue  A queue is a FIFO (First-In, First-Out) list. Bus Stop front rear rear
  • 8. A Print Queue  Print queue: printing is much slower than the process of selecting pages to print and so a queue is used
  • 9. The Unsuitability of a Print Stack  Stacks are last-in, first-out (LIFO)  The most recently selected document would be the next to print  Unless the printer stack is empty, your print job may never get executed if others are issuing print jobs.
  • 10.   Program #1 Program #2front rear Send a message to the queue Take a message from the queue Message queues in an operating system  There are times that programs need to communicate with each other. Unix operating system provides message queue as one of the mechanisms to facilitate communication.
  • 11. Example: Web spider  Search engines (Altavista, Google, etc) uses programs called spiders to discover new pages on the web.  Input = some particular seed page  Repeatedly: select a previously-discovered page, and traverse one of its hyperlinks. To ensure broad coverage of the entire web, spiders employ a queue data-structure to store & select discovered pages next page to be explored newly discovered pages
  • 12. Basic Queue Operations  Enqueue: Insertion of an element at rear of queue  Dequeue: deletion of an element at the front of queue  Queue front: examines the element at front of queue  Queue rear: examines element at rear of queue
  • 14.
  • 15. Exercise  What would be the contents of queue Q after the following code is executed and the following data are entered?Q = createQueue loop (not end of file) read number if (number not 0) enqueue(Q, number) else dequeue(Q) end if end loop Data:  5,  7,  12,  4,  0,  4,   8,  67,  23, 0,  44,   33,  6,  0
  • 16. Queue Implementation Issues  Consider a queue implemented in an array of size 10  After 5 e nq ue ue operations, the shaded portion of the array has been filled: 0 1 2 9 front rear
  • 17. Queue Implementation Issues  After two de q ue ue operations, if array contents are not shifted, we have: 0 1 2 9 front rear • The queue capacity is now reduced to 8 items
  • 18. Queue Implementation Issues  Option 1 using an underlying array: enhance the e nq ue ue operation to increase the size of the underlying array whenever there is an item in the final array location:  de q ue ue is a fast operation; e nq ue ue is fast except when the array needs replacing  Could use a large amount of space: once an item is removed, its array location is never reused
  • 19. Queue Implementation Issues  Option 2  Shift all the remaining items at each de q ue ue operation
  • 20. Circular Queues  We can implement fixed-capacity queues using arrays so that the queue capacity never decreases: use circularqueues  Concept can be extended so that the underlying array can be replaced by a larger array if necessary
  • 21. Circular Queues  Treat the underlying array as if it is circular: in an n-element array, the location “before” index 0 is index n-1; the location “after” index n-1 is index 0 1 0 12 11 10 • Use modular arithmetic to compute next and previous indices
  • 22. Circular Queues 1 0 12 11 10 1 0 12 11 10 1 0 12 11 10 After 7 enqueues front rear After 5 dequeues front rear After 8 more enqueues front rear
  • 23. Array Based Queue Implementation  template <class Qtype> class Queue {  Qtype queue[SIZE];  int front, rear;  public:  Queue() {  front = rear= -1;  }  void enqueue(Qtype num);  Qtype dequeue();  bool isfull();  Qtype isempty();  };
  • 24. isfull  template <class Qtype> bool Queue<Qtype>::isfull()  {  if(rear+1==front || (rear+1==SIZE && front==0)) {  return true;  }  else{  return false;  }  }
  • 25. isempty  template <class Qtype> bool Queue<Qtype>::isempty()  {  if(rear==-1)  return true;  }  else{  return false;  }  }
  • 26. enqueue  template <class Qtype> void Queue<Qtype>::enqueue(Qtype num)  {  if(isfull()) {  cout << "Queue is full.n";  }  else{  if(front==-1){  front++;  }  rear = (rear + 1) % SIZE;  queue[rear] = item;  }  }
  • 27. dequeue  template <class Qtype> Qtype Queue<Qtype>::dequeue()  {  if(isempty()) {  cout << "Queue is empty.n";  return -999;  }  else{  data=queue[front];  if(front==rear){  read=front=-1;  }  else{  front = (front + 1) % SIZE;  }  return data;  }  }
  • 28. Priority Queues  Works like an ordinary queue.  However, the order of how things are removed depend on a priority key.  Ascending Priority Queue – The item with the smallest key has the highest priority.  Descending Priority Queue – The item with the largest key has the highest priority.  They will be covered after later.
  • 29. Queues vs. Stacks  Stacks are a LIFO container => store data in the reverse of order received  Queues are a FIFO container => store data in the order received  Stacks then suggest applications where some sort of reversal or unwinding is desired.  From an ordering perspective, then, Queue are the “opposite” of stacks  Easy solution to the palindrome problem
  • 30. End

Editor's Notes

  1. This is FIFO queue. Some real life queues are not FIFO. Removal from a queue may be done by priority rather than by arrival time order. For example, loading into a plane– first class, frequent fliers, by rows from back of plane rather than by order in which you arrive at the departure gate.