SlideShare a Scribd company logo
1 of 11
Download to read offline
Queues
Hareem Aslam
Queues:
 A queue is a data structure that models/enforces the first-come
first-serve order, or equivalently the first-in first-out (FIFO) order.
 That is, the element that is inserted first into the queue will be the
element that will deleted first, and the element that is inserted last is
deleted last.
 A waiting line is a good real-life example of a queue.
Hareem Aslam
A Graphic Model of a Queue
Tail:
All new items
are added on
this end
Head:
All items are
deleted from
this end
Hareem Aslam
Examples of Queues
 An electronic mailbox is a queue
 A waiting line in a store, at a service counter, on a one-lane road
 Equal-priority processes waiting to run on a processor in a
computer system
 Radix Sort
Hareem Aslam
How head and tail Change?
 head increases by 1 after each dequeue( )
 tail increases by 1 after each enqueue( )
0124 3
0124 3
0124 3
Now:
After enqueue:
After dequeue:
tail head
tail head
tail head
Hareem Aslam
Queues Algorithm[1]
Enqueue:
1. ENQUEUE(Q,x)
2. Q[Q.tail] = x
3. If Q.tail == Q.length
4. Q.tail = 1
5. else Q.tail = Q.tail + 1
Hareem Aslam
Queues Algorithm[2]
Dequeue:
DEQUEUE(Q)
1. x = Q[Q.head]
2. If Q.head == q.length
3. Q.head = 1
4. else Q.head = Q.head + 1
5. return x
Hareem Aslam
Problem
 Suppose 50 calls to enqueue have been made, so now the queue array
is full
 Assume 4 calls to dequeue( ) are made
 Assume a call to enqueue( ) is made now. The tail part seems to have
no space, but the front has 4 unused spaces; if never used, they are
wasted.
012474849
012474849
34
34
tail head
tail head
Hareem Aslam
Solution: A Circular Queue
 Allow the head (and the tail) to be moving targets
 When the tail end fills up and front part of the
array has empty slots, new insertions should go into
the front end
 Next insertion goes into slot 0, and tail tracks it.
The insertion after that goes into a lot 1, etc.
012474849
headtail
34
Hareem Aslam
Illustration of Circular Queues
 Current state:
 After One Call to enqueue()
 After One Call to enqueue()
012474849 34
tail
head
012474849 34
tailhead
012474849 34
head tail
Hareem Aslam
Numerics for Circular Queues
 head increases by (1 modulo capacity) after each dequeue( )
head = (head +1) % capacity;
 tail increases by (1 modulo capacity) after each enqueue( )
tail = (tail +1) % capacity;
Hareem Aslam

More Related Content

What's hot

Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 

What's hot (20)

Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Heaps
HeapsHeaps
Heaps
 
Array data structure
Array data structureArray data structure
Array data structure
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Merge sort
Merge sortMerge sort
Merge sort
 
Heaps
HeapsHeaps
Heaps
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Queues
QueuesQueues
Queues
 
Unit I-Data structures stack & Queue
Unit I-Data structures stack & QueueUnit I-Data structures stack & Queue
Unit I-Data structures stack & Queue
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
Queue
QueueQueue
Queue
 
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
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
stack & queue
stack & queuestack & queue
stack & queue
 
Python Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptxPython Data Structures and Algorithms.pptx
Python Data Structures and Algorithms.pptx
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 

Viewers also liked

Sem3 nivel1 francia romano terra
Sem3 nivel1 francia romano terraSem3 nivel1 francia romano terra
Sem3 nivel1 francia romano terra
moncayo1986
 

Viewers also liked (20)

Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queue data structure
Queue data structureQueue data structure
Queue 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)
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queue
QueueQueue
Queue
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
Preparation Data Structures 08 queues
Preparation Data Structures 08 queuesPreparation Data Structures 08 queues
Preparation Data Structures 08 queues
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Applications of data structures
Applications of data structuresApplications of data structures
Applications of data structures
 
Data structure
Data structureData structure
Data structure
 
Sem3 nivel1 francia romano terra
Sem3 nivel1 francia romano terraSem3 nivel1 francia romano terra
Sem3 nivel1 francia romano terra
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
 
Queues
QueuesQueues
Queues
 
Materi Struktur data QUEUE
Materi Struktur data QUEUEMateri Struktur data QUEUE
Materi Struktur data QUEUE
 

Similar to Queues

Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
abinathsabi
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 

Similar to Queues (20)

Queue ADT for data structure for computer
Queue ADT for data structure for computerQueue ADT for data structure for computer
Queue ADT for data structure for computer
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
6 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart36 chapter3 list_stackqueuepart3
6 chapter3 list_stackqueuepart3
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
Queue
QueueQueue
Queue
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
stack.ppt
stack.pptstack.ppt
stack.ppt
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Queues
QueuesQueues
Queues
 
Lecture 2d queues
Lecture 2d queuesLecture 2d queues
Lecture 2d queues
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Queue
QueueQueue
Queue
 
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdfCEN 235 4. Abstract Data Types - Queue and Stack.pdf
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
 
Unit II - LINEAR DATA STRUCTURES
Unit II -  LINEAR DATA STRUCTURESUnit II -  LINEAR DATA STRUCTURES
Unit II - LINEAR DATA STRUCTURES
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 

More from Hareem Aslam

More from Hareem Aslam (20)

Chapter 1 Basic Concepts
Chapter 1 Basic ConceptsChapter 1 Basic Concepts
Chapter 1 Basic Concepts
 
Solution Manual : Chapter - 07 Exponential, Logarithmic and Inverse Trigonome...
Solution Manual : Chapter - 07 Exponential, Logarithmic and Inverse Trigonome...Solution Manual : Chapter - 07 Exponential, Logarithmic and Inverse Trigonome...
Solution Manual : Chapter - 07 Exponential, Logarithmic and Inverse Trigonome...
 
Solution Manual : Chapter - 06 Application of the Definite Integral in Geomet...
Solution Manual : Chapter - 06 Application of the Definite Integral in Geomet...Solution Manual : Chapter - 06 Application of the Definite Integral in Geomet...
Solution Manual : Chapter - 06 Application of the Definite Integral in Geomet...
 
Solution Manual : Chapter - 05 Integration
Solution Manual : Chapter - 05 IntegrationSolution Manual : Chapter - 05 Integration
Solution Manual : Chapter - 05 Integration
 
Solution Manual : Chapter - 02 Limits and Continuity
Solution Manual : Chapter - 02 Limits and ContinuitySolution Manual : Chapter - 02 Limits and Continuity
Solution Manual : Chapter - 02 Limits and Continuity
 
Solution Manual : Chapter - 01 Functions
Solution Manual : Chapter - 01 FunctionsSolution Manual : Chapter - 01 Functions
Solution Manual : Chapter - 01 Functions
 
Personality Development
Personality DevelopmentPersonality Development
Personality Development
 
Chapter 21 c language
Chapter 21 c languageChapter 21 c language
Chapter 21 c language
 
Chapter 20 coc
Chapter 20 cocChapter 20 coc
Chapter 20 coc
 
Chapter 19 multimedia
Chapter 19 multimediaChapter 19 multimedia
Chapter 19 multimedia
 
Chapter 18 internet
Chapter 18 internetChapter 18 internet
Chapter 18 internet
 
Chapter 16 bdp
Chapter 16 bdpChapter 16 bdp
Chapter 16 bdp
 
Chapter 17 dccn
Chapter 17 dccnChapter 17 dccn
Chapter 17 dccn
 
Chapter 15 asp
Chapter 15 aspChapter 15 asp
Chapter 15 asp
 
Chapter 14 os
Chapter 14 osChapter 14 os
Chapter 14 os
 
Chapter 13 sio
Chapter 13 sioChapter 13 sio
Chapter 13 sio
 
Chapter 12 cl
Chapter 12 clChapter 12 cl
Chapter 12 cl
 
Chapter 11 pcp
Chapter 11 pcpChapter 11 pcp
Chapter 11 pcp
 
Chapter 10 cs
Chapter 10 csChapter 10 cs
Chapter 10 cs
 
Chapter 09 io devices
Chapter 09 io devicesChapter 09 io devices
Chapter 09 io devices
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Queues

  • 2. Queues:  A queue is a data structure that models/enforces the first-come first-serve order, or equivalently the first-in first-out (FIFO) order.  That is, the element that is inserted first into the queue will be the element that will deleted first, and the element that is inserted last is deleted last.  A waiting line is a good real-life example of a queue. Hareem Aslam
  • 3. A Graphic Model of a Queue Tail: All new items are added on this end Head: All items are deleted from this end Hareem Aslam
  • 4. Examples of Queues  An electronic mailbox is a queue  A waiting line in a store, at a service counter, on a one-lane road  Equal-priority processes waiting to run on a processor in a computer system  Radix Sort Hareem Aslam
  • 5. How head and tail Change?  head increases by 1 after each dequeue( )  tail increases by 1 after each enqueue( ) 0124 3 0124 3 0124 3 Now: After enqueue: After dequeue: tail head tail head tail head Hareem Aslam
  • 6. Queues Algorithm[1] Enqueue: 1. ENQUEUE(Q,x) 2. Q[Q.tail] = x 3. If Q.tail == Q.length 4. Q.tail = 1 5. else Q.tail = Q.tail + 1 Hareem Aslam
  • 7. Queues Algorithm[2] Dequeue: DEQUEUE(Q) 1. x = Q[Q.head] 2. If Q.head == q.length 3. Q.head = 1 4. else Q.head = Q.head + 1 5. return x Hareem Aslam
  • 8. Problem  Suppose 50 calls to enqueue have been made, so now the queue array is full  Assume 4 calls to dequeue( ) are made  Assume a call to enqueue( ) is made now. The tail part seems to have no space, but the front has 4 unused spaces; if never used, they are wasted. 012474849 012474849 34 34 tail head tail head Hareem Aslam
  • 9. Solution: A Circular Queue  Allow the head (and the tail) to be moving targets  When the tail end fills up and front part of the array has empty slots, new insertions should go into the front end  Next insertion goes into slot 0, and tail tracks it. The insertion after that goes into a lot 1, etc. 012474849 headtail 34 Hareem Aslam
  • 10. Illustration of Circular Queues  Current state:  After One Call to enqueue()  After One Call to enqueue() 012474849 34 tail head 012474849 34 tailhead 012474849 34 head tail Hareem Aslam
  • 11. Numerics for Circular Queues  head increases by (1 modulo capacity) after each dequeue( ) head = (head +1) % capacity;  tail increases by (1 modulo capacity) after each enqueue( ) tail = (tail +1) % capacity; Hareem Aslam