SlideShare a Scribd company logo
Introduction To 
Algorithm
Abstract Data Type 
Stack & Queue is an example of ADT 
An array is not ADT.
Using a Queue 
Several example 
applications of queues are 
given in that Slides. 
This presentation describes 
the queue operations and 
two ways to implement a 
queue. 
Data Structures 
and Other Objects 
Using C++
What is Queue? 
A QUEUE IS A CONTAINER IN WHICH INSERTIONS 
ARE MADE ONLY AT THE BACK DELETIONS, 
RETRIEVALS, AND MODIFICATIONS ARE MADE 
ONLY AT THE FRONT.
The Queue Operations 
A queue is like a line of 
people waiting for a 
bank teller. The queue 
has a front and a rear. 
$ $ 
Front 
Rear
EnQueue Operations 
New people must enter the queue at 
the rear. The C++ queue class calls 
this a push, although it is usually 
called an enqueue operation. 
$ $ 
Front 
Rear
DeQueue Operations 
 When an item is taken from the queue, 
it always comes from the front. The C++ 
queue calls this a pop, although it is 
usually called a dequeue operation. 
$ $ 
Front 
Rear
Array Implementation 
A queue can be implemented with an array, as 
shown here. For example, this queue contains 
the integers 4 (at the front), 8 and 6 (at the rear). 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6 
AAnn aarrrraayy ooff iinntteeggeerrss ttoo 
iimmpplleemmeenntt aa qquueeuuee ooff 
iinntteeggeerrss 
WWee ddoonn''tt ccaarree wwhhaatt''ss iinn 
tthhiiss ppaarrtt ooff tthhee aarrrraayy..
Array Implementation 
The efficient implementation also keeps 
track of the number of items in the 
queue and the index of the first element 
(at the front of the queue), the last 
element (at the rear). 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6 
3 ssiizzee 
0 ffiirrsstt 
2 llaasstt
A Dequeue Operation 
When an element leaves the queue, 
size is decremented, and first 
changes, too. 
2 ssiizzee 
1 ffiirrsstt 
2 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6
An Enqueue Operation 
When an element enters the queue, 
size is incremented, and last changes, 
too. 
3 ssiizzee 
1 ffiirrsstt 
3 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
8 6 2
At the End of the Array 
There is special behaviour at the end 
of the array. For example, suppose we 
want to add a new element to this 
queue, where the last index is [5]: 
2 
3 ssiizzee 
3 ffiirrsstt 
5 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 
5 6 1
At the End of the Array 
The new element goes at the front of 
the array (if that spot isn’t already 
used): 
4 ssiizzee 
3 ffiirrsstt 
0 llaasstt 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 
4 2 6 1
Array Queue Reviews 
 Easy to implement. 
 But it has a limited capacity with a fixed array 
 Special behaviour is needed when the rear 
reaches the end of the array. 
[[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 
4 8 6 
3 ssiizzee 
0 ffiirrsstt 
2 llaasstt
Linked List Implementation 
A queue can also be 
implemented with a linked 
list with both a head and a 
tail pointer. 
null 
10 
15 
7 
13 
Head-ptr 
Tail-ptr
Linked List Implementation 
Which end do you think is the 
front of the queue? Why ? 
10 
15 
7 
13 
Head-ptr 
Tail-ptr 
null
Linked List Implementation 
The head-ptr points to the 
front of the list. 
Because it is harder to 
remove items from the tail 
of the list. 
Rear 
Front 
10 
15 
7 
13 
Head-ptr 
Tail-ptr 
null
The Queue Class 
template <class Item> 
class queue<Item> 
{ 
public: 
queue( ); 
void push(const Item& entry); 
void pop( ); 
bool empty( ) const; 
Item front( ) const; 
} 
 The C++ standard 
template library has a 
queue template class. 
 The template parameter 
is the type of the items 
that can be put in the 
queue.
Summary 
QQuueeuueess hhaavvee mmaannyy aapppplliiccaattiioonnss.. 
 IItteemmss eenntteerr aa qquueeuuee aatt tthhee rreeaarr aanndd 
lleeaavvee aa qquueeuuee aatt tthhee ffrroonntt.. 
QQuueeuueess ccaann bbee iimmpplleemmeenntteedd uussiinngg aann 
aarrrraayy oorr uussiinngg aa lliinnkkeedd lliisstt..
Daily Life Examples 
There's a queue of questions 
that you've asked in exam, all 
waiting for you to accept 
answers for them. 
Waiting in a queue in any 
bank for paying bills, depositing 
cash OR cheques.
Thank You 
THE END

More Related Content

PPTX
Doubly Linked List
PPT
Queue Data Structure
PPTX
Queue ppt
PPTX
Stacks and Queue - Data Structures
PPT
stack and queue array implementation in java.
PPTX
Queue in Data Structure
PPTX
Circular linked list
PPSX
Data structure stack&queue basics
Doubly Linked List
Queue Data Structure
Queue ppt
Stacks and Queue - Data Structures
stack and queue array implementation in java.
Queue in Data Structure
Circular linked list
Data structure stack&queue basics

What's hot (20)

PPTX
PPTX
Queue
PPT
Selection sort
PPTX
Infix to postfix conversion
PPT
Queue Data Structure
PPTX
My lectures circular queue
PPTX
Counting Sort
PDF
Queue as data_structure
PPTX
Queues in C++
PPTX
Stack and Queue
PPTX
Quick sort-Data Structure
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPTX
stack & queue
PPTX
PPTX
PPTX
Data Structures - Lecture 7 [Linked List]
PPTX
Java Queue.pptx
PPTX
PPTX
Binary Search Tree
Queue
Selection sort
Infix to postfix conversion
Queue Data Structure
My lectures circular queue
Counting Sort
Queue as data_structure
Queues in C++
Stack and Queue
Quick sort-Data Structure
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
stack & queue
Data Structures - Lecture 7 [Linked List]
Java Queue.pptx
Binary Search Tree
Ad

Viewers also liked (20)

PPT
Queue data structure
PPTX
Ppt presentation of queues
PPT
Notes DATA STRUCTURE - queue
PPTX
Queue Data Structure (w/ php egs)
PPTX
STACKS IN DATASTRUCTURE
PPTX
queue & its applications
PPSX
PPT
DATA STRUCTURES
PPT
Queue and stacks
PPT
Priority queues
PPT
Algorithm: priority queue
PPTX
Trees (data structure)
PPTX
Trees data structure
PDF
computer notes - Priority queue
PPT
Heaps & priority queues
PPT
Stack & queue
PPTX
stack
PPTX
Priority queue
Queue data structure
Ppt presentation of queues
Notes DATA STRUCTURE - queue
Queue Data Structure (w/ php egs)
STACKS IN DATASTRUCTURE
queue & its applications
DATA STRUCTURES
Queue and stacks
Priority queues
Algorithm: priority queue
Trees (data structure)
Trees data structure
computer notes - Priority queue
Heaps & priority queues
Stack & queue
stack
Priority queue
Ad

Similar to Queue (20)

PPT
basics of queues
PPT
Lecture 2d queues
PDF
Queue ADT for data structure for computer
PPTX
PPTX
Queue Data Structure with detailed explanation
PPT
Lec-07 Queues.ppt queues introduction to queue
PPTX
Mca ii dfs u-3 linklist,stack,queue
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPT
Queue data structure
PPT
Data Structure Lecture 4
PPTX
Queues_0748555555555555555555555526.pptx
PPT
Queue AS an ADT (Abstract Data Type)
PPT
Queues in C++ detailed explanation and examples .ppt
PPTX
Fundamentals of Data Structure and Queues
PPTX
Unit 4 queue
PPTX
PDF
Lesson 4 - Queue ADT.pdf
PPTX
Dynamic Queue.pptx
basics of queues
Lecture 2d queues
Queue ADT for data structure for computer
Queue Data Structure with detailed explanation
Lec-07 Queues.ppt queues introduction to queue
Mca ii dfs u-3 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Queue data structure
Data Structure Lecture 4
Queues_0748555555555555555555555526.pptx
Queue AS an ADT (Abstract Data Type)
Queues in C++ detailed explanation and examples .ppt
Fundamentals of Data Structure and Queues
Unit 4 queue
Lesson 4 - Queue ADT.pdf
Dynamic Queue.pptx

Recently uploaded (20)

PDF
Swiggy’s Playbook: UX, Logistics & Monetization
PPTX
Hydrogel Based delivery Cancer Treatment
PPTX
The Effect of Human Resource Management Practice on Organizational Performanc...
PPTX
Intro to ISO 9001 2015.pptx wareness raising
PPTX
Impressionism_PostImpressionism_Presentation.pptx
PPTX
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
PDF
Why Top Brands Trust Enuncia Global for Language Solutions.pdf
PPTX
An Unlikely Response 08 10 2025.pptx
PPTX
Primary and secondary sources, and history
PPTX
The spiral of silence is a theory in communication and political science that...
PDF
oil_refinery_presentation_v1 sllfmfls.pdf
PPTX
Human Mind & its character Characteristics
PPTX
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
PPTX
Emphasizing It's Not The End 08 06 2025.pptx
PPTX
Learning-Plan-5-Policies-and-Practices.pptx
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PPTX
Role and Responsibilities of Bangladesh Coast Guard Base, Mongla Challenges
PDF
Instagram's Product Secrets Unveiled with this PPT
DOCX
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
PDF
Nykaa-Strategy-Case-Fixing-Retention-UX-and-D2C-Engagement (1).pdf
Swiggy’s Playbook: UX, Logistics & Monetization
Hydrogel Based delivery Cancer Treatment
The Effect of Human Resource Management Practice on Organizational Performanc...
Intro to ISO 9001 2015.pptx wareness raising
Impressionism_PostImpressionism_Presentation.pptx
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
Why Top Brands Trust Enuncia Global for Language Solutions.pdf
An Unlikely Response 08 10 2025.pptx
Primary and secondary sources, and history
The spiral of silence is a theory in communication and political science that...
oil_refinery_presentation_v1 sllfmfls.pdf
Human Mind & its character Characteristics
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
Emphasizing It's Not The End 08 06 2025.pptx
Learning-Plan-5-Policies-and-Practices.pptx
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
Role and Responsibilities of Bangladesh Coast Guard Base, Mongla Challenges
Instagram's Product Secrets Unveiled with this PPT
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
Nykaa-Strategy-Case-Fixing-Retention-UX-and-D2C-Engagement (1).pdf

Queue

  • 2. Abstract Data Type Stack & Queue is an example of ADT An array is not ADT.
  • 3. Using a Queue Several example applications of queues are given in that Slides. This presentation describes the queue operations and two ways to implement a queue. Data Structures and Other Objects Using C++
  • 4. What is Queue? A QUEUE IS A CONTAINER IN WHICH INSERTIONS ARE MADE ONLY AT THE BACK DELETIONS, RETRIEVALS, AND MODIFICATIONS ARE MADE ONLY AT THE FRONT.
  • 5. The Queue Operations A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ Front Rear
  • 6. EnQueue Operations New people must enter the queue at the rear. The C++ queue class calls this a push, although it is usually called an enqueue operation. $ $ Front Rear
  • 7. DeQueue Operations  When an item is taken from the queue, it always comes from the front. The C++ queue calls this a pop, although it is usually called a dequeue operation. $ $ Front Rear
  • 8. Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 AAnn aarrrraayy ooff iinntteeggeerrss ttoo iimmpplleemmeenntt aa qquueeuuee ooff iinntteeggeerrss WWee ddoonn''tt ccaarree wwhhaatt''ss iinn tthhiiss ppaarrtt ooff tthhee aarrrraayy..
  • 9. Array Implementation The efficient implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 3 ssiizzee 0 ffiirrsstt 2 llaasstt
  • 10. A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. 2 ssiizzee 1 ffiirrsstt 2 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6
  • 11. An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. 3 ssiizzee 1 ffiirrsstt 3 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 8 6 2
  • 12. At the End of the Array There is special behaviour at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: 2 3 ssiizzee 3 ffiirrsstt 5 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 5 6 1
  • 13. At the End of the Array The new element goes at the front of the array (if that spot isn’t already used): 4 ssiizzee 3 ffiirrsstt 0 llaasstt [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] 4 2 6 1
  • 14. Array Queue Reviews  Easy to implement.  But it has a limited capacity with a fixed array  Special behaviour is needed when the rear reaches the end of the array. [[ 00 ]] [[11]] [[ 22 ]] [[ 33 ]] [[ 44 ]] [[ 55 ]] .. .. .. 4 8 6 3 ssiizzee 0 ffiirrsstt 2 llaasstt
  • 15. Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail pointer. null 10 15 7 13 Head-ptr Tail-ptr
  • 16. Linked List Implementation Which end do you think is the front of the queue? Why ? 10 15 7 13 Head-ptr Tail-ptr null
  • 17. Linked List Implementation The head-ptr points to the front of the list. Because it is harder to remove items from the tail of the list. Rear Front 10 15 7 13 Head-ptr Tail-ptr null
  • 18. The Queue Class template <class Item> class queue<Item> { public: queue( ); void push(const Item& entry); void pop( ); bool empty( ) const; Item front( ) const; }  The C++ standard template library has a queue template class.  The template parameter is the type of the items that can be put in the queue.
  • 19. Summary QQuueeuueess hhaavvee mmaannyy aapppplliiccaattiioonnss..  IItteemmss eenntteerr aa qquueeuuee aatt tthhee rreeaarr aanndd lleeaavvee aa qquueeuuee aatt tthhee ffrroonntt.. QQuueeuueess ccaann bbee iimmpplleemmeenntteedd uussiinngg aann aarrrraayy oorr uussiinngg aa lliinnkkeedd lliisstt..
  • 20. Daily Life Examples There's a queue of questions that you've asked in exam, all waiting for you to accept answers for them. Waiting in a queue in any bank for paying bills, depositing cash OR cheques.

Editor's Notes

  • #3: When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive.
  • #4: This lecture introduces queues. The presentation also shows two common ways of implementing a queue of integers.
  • #5: When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive.
  • #6: When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive.
  • #7: Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear.
  • #8: When an item is removed from a queue, the removal occurs at the front.
  • #9: Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array.
  • #10: The easiest implementation also keeps track of three numbers. The size could be as small as zero or as large as the number of items in the array. The index of the front element is stored in the first member variable. The front item in the queue is at that index of the array. The next item is after the first one and so on until the rear of the queue that occurs at the index stored in a member variable called last.
  • #11: This shows how the member variables change when an item leaves the queue.
  • #12: And this shows how the member variables change when a new item enters the queue. For a fixed size array, a new item may enter only if the current size of the queue is less than the size of the array. For a dynamic array, we could increase the size of the array when the queue grows beyond the current array size.
  • #13: An array implementation of a queue must have special behavior when the rear of the queue reaches the end of the array. In this example, suppose we want to add the number 4 to the queue. We can do so…
  • #14: …by putting it at location 0 (if that location is not already used).
  • #15: Here are some of the key aspects of an array implementation of a queue.
  • #16: A linked list can also be used to implement a queue, but we must maintain both a head and a tail pointer because we need access to both the front and the rear of the queue.
  • #17: Does it matter which end of a singly-linked list we use for the front of the queue?
  • #18: Of course, we could put the front of the queue at the end of the linked list, but it would be hard to remove an item. Do you see why?
  • #19: These are the four most common queue operations. The empty function tells you whether the queue has any items at the moment. The front operation returns the item at the front of the queue (without removing it from the queue).
  • #20: A quick summary . . .
  • #22: Feel free to send your ideas to: Michael Main [email_address]