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

Queue

  • 1.
  • 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 Newpeople 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 Aqueue 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 Theefficient 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 Endof 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 Endof 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 hhaavveemmaannyy 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.
  • 21.

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]