Using a Queue
We introduce the
We introduce the queue
queue
data type.
data type.
Several example
Several example
applications of queues
applications of queues
are given in that
are given in that
chapter.
chapter.
This presentation
This presentation
describes the queue
describes the queue
operations and two ways
operations and two ways
to implement a queue.
to implement a queue.
2.
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
3.
The Queue 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
4.
The Queue 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
5.
The Queue Class
The C++ standard
The C++ standard
template library has
template library has
a queue template
a queue template
class.
class.
The template
The template
parameter is the
parameter is the
type of the items
type of the items
that can be put in
that can be put in
the queue.
the queue.
template <class Item>
class queue<Item>
{
public:
queue( );
void push(const Item& entry);
void pop( );
bool empty( ) const;
Item front( ) const;
…
6.
Overflow vs underflow
If a program attempts to add an
entry to a queue that is already at
its capacity, this is, of course, an
error.
This error is called queue overflow.
If a program attempts to remove
an entry from an empty queue,
that is another kind of error, called
queue underflow
7.
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).
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ] . . .
. . .
An array of integers
An array of integers
to implement a
to implement a
queue of integers
queue of integers
4 8 6
We don't care what's in
We don't care what's in
this part of the array.
this part of the array.
8.
Array Implementation
Theeasiest 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).
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ] . . .
. . .
4 8 6
size
size
3
first
first
0
last
last
2
9.
A Dequeue Operation
When an element leaves the
queue, size is decremented,
and first changes, too.
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ] . . .
. . .
4 8 6
size
size
2
first
first
1
last
last
2
10.
An Enqueue Operation
When an element enters
the queue, size is
incremented, and last
changes, too.
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ] . . .
. . .
2
8 6
size
size
3
first
first
1
last
last
3
11.
At the Endof the Array
There is special behavior 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]:
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ]
2 1
6
size
size
3
first
first
3
last
last
5
12.
At the Endof the Array
The new element goes at
the front of the array (if that
spot isn’t already used):
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ]
2 1
6
size
size
4
first
first
3
last
last
0
4
13.
Array Implementation
Easyto implement
But it has a limited capacity with a fixed
array
Special behavior is needed when the
rear reaches the end of the array.
[ 0 ]
[ 0 ] [1]
[1] [ 2 ]
[ 2 ] [ 3 ]
[ 3 ] [ 4 ]
[ 4 ] [ 5 ]
[ 5 ] . . .
. . .
4 8 6
size
size
3
first
first
0
last
last
2
14.
Linked List Implementation
A queue can also be
implemented with a linked
list with both a head and a
tail pointer.
10
15
7
null
13
head_ptr
tail_ptr
15.
Linked List Implementation
Which end do you think is
the front of the queue?
Why?
10
15
7
null
13
head_ptr
tail_ptr
16.
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.
10
15
7
null
head_ptr
13
tail_ptr
Front
Rear
Suppose youwant a program to read a
word and then write the word.
One way to accomplish this task is to
read the input one letter at a time and
place each letter in a queue.
After the word is read, the letters in the
queue are written out.
Because a queue is a First-In/First-Out
data structure, the letters are written in
the same order in which they were read.
The pseudocode for this approach to
copying a word is as follows:
20.
Recognizing Palindromes
Apalindrome is a string that reads
the same forward and backward;
that is, the letters are the same
whether you read them from right
to left or from left to right.
For example, the one-word string
“radar” is a palindrome.
Able was I ere I saw Elba
21.
We cando this by using both a stack and a
queue.
We will read the line of text into both a stack and
a queue
The program can simply compare the contents of
the stack and the queue character-by-character
to see if they would produce the same string of
characters.
Summary
Like stacks,queues have many
Like stacks, queues have many
applications.
applications.
Items enter a queue at the rear and
Items enter a queue at the rear and
leave a queue at the front.
leave a queue at the front.
Queues can be implemented using
Queues can be implemented using
an array or using a linked list.
an array or using a linked list.
26.
Suppose thatlocate_ptr is a pointer to a node in
a linked list (and it is not the null pointer). Write a
statement that will make locate_ptr move to the
next node in the list. What does your statement
do if locate_ptr was already pointing to the last
node in the list?
Write a statement to correctly set the tail pointer
of a list when a new first node has been added
to the list. Assume that head_ptr points to the
new first node.
Suppose that head_ptr is a head pointer for a
linked list with just one node. What will head_ptr
be after list_head_remove(head_ptr)?
27.
Consider the followingpseudocode: declare a
stack of characters
while ( there are more characters in the word to
read )
{ read a character push the character on the
stack }
while ( the stack is not empty )
{ write the stack's top character to the screen
pop a character off the stack }
What is written to the screen for the input
"carpets"?
28.
Exercice
Write pseudocodefor an algorithm that reads an
even number of characters. The algorithm then
prints the first character, third character, fifth
character, and so on. On a second output line,
the algorithm prints the second character, fourth
character, sixth character, and so on. Use two
queues to store the characters.
Editor's Notes
#1 This lecture introduces queues. The presentation also shows two common ways of implementing a queue of integers.
#2 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.
#3 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.
#4 When an item is removed from a queue, the removal occurs at the front.
#5 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).
#7 Just like our stack implementation in the previous chapter, one way to implement a queue is to store the elements in an array.
#8 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.
#9 This shows how the member variables change when an item leaves the queue.
#10 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.
#11 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…
#12 …by putting it at location 0 (if that location is not already used).
#13 Here are some of the key aspects of an array implementation of a queue.
#14 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.
#15 Does it matter which end of a singly-linked list we use for the front of the queue?
#16 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?