Title: Queues
1Queues
- (Walls Mirrors - Chapter 7)
2Overview
- The ADT Queue
- Linked-List Implementation of a Queue
- Array Implementation of a Queue
3The ADT Queue
- A queue is a linear list, for which all
insertions are made at one end of the list all
deletions are made at the other end. Queues
exhibit a First In First Out (FIFO) property. - This is like the queue (or line) at a movie
theater, checkout counter at a store, bus stop,
etc. When you arrive, you go to the back of the
queue. People are served in the order that they
arrive, starting at the front.
4The ADT Queue (Contd.)
- For an ADT Queue, the supported operations
include - Create an empty queue
- Destroy a queue
- Determine whether a queue is empty
- Add a new item to the queue
- Remove the item added earliest
- Retrieve a copy of the item added earliest
- As long as these operations are supported, you
dont need to be concerned with whether the queue
is implemented as an array, a linked list, or
something else.
5Comparing Stacks and Queues
- Recall, that a stack is a linear list, for which
all insertions and deletions occur at the same
end of the list, whereas insertions and deletions
occur at opposite ends of a queue. - Stacks exhibit a Last In First Out (LIFO)
property queues exhibit a First In First Out
(FIFO) property - In a stack, remove and retrieve act on the the
item added most recently - In a queue, remove and retrieve act on the item
added earliest.
6ADT Queue Public Member Functions
- Queue( )
- is the default constructor, which creates a new,
empty queue. - Queue( const Queue oQueue )
- is the copy constructor, which makes a copy of
oQueue. - Queue( )
- is the destructor, which destroys a queue.
- bool isEmpty( ) const
- returns true if the queue is empty, otherwise
returns false.
7ADT Queue Public Member Functions
- bool enqueue( QueueItemType newItem )
- adds newItem at the back of a queue. Returns
true if enqueue succeeds, otherwise returns
false. - bool dequeue( )
- removes the item at the front of a queue.
Returns true if dequeue succeeds, otherwise
returns false. - bool dequeue( QueueItemType queueFront )
- removes the item at the front of a queue and
returns it in queueFront. Returns true if
dequeue succeeds, otherwise returns false.
8ADT Queue Public Member Functions
- bool getFront( QueueItemType queueFront )
const - retrieves a copy of the item at the front of a
queue and returns it in queueFront, leaving the
queue unchanged. Returns true if getFront
succeeds, otherwise returns false.
9Application Recognizing Palindromes
- Recall, that a palindrome is a string that reads
the same from left to right as right to left. - NOON, DEED, RADAR, MADAM
- ABLE WAS I ERE I SAW ELBA
- One way to recognize a palindrome is to copy the
candidate string into both a stack and a queue.
Remove the characters one at a time from the
stack and queue and compare - If a difference is found, the candidate string is
not a palindrome. - If all characters match, the candidate string is
a palindrome.
10Recognizing PalindromesRADAR
Step 1 Copy RADAR into Queue and Stack
11Recognizing PalindromesRADAR
Step 2 Remove RADAR from Queue and Stack
12Recognizing Palindromes
- bool isPal( str char )
-
- Queue charQueue Stack charStack
- // copy each character of str into both
charQueue and charStack - bool match char queueFront, stackTop
- // set match to true if characters removed
from charQueue match - // characters removed from charStack
otherwise set match to false - return match
13Recognizing Palindromes
- // copy each character of str into both
charQueue and charStack - for( int i 0 i lt strlen( str ) 1 i
) - charQueue.enqueue( stri )
- charStack.push( stri )
-
// set match to true if characters removed from
charQueue match // characters removed from
charStack otherwise set match to false for(
match true match !charQueue.isEmpty( )
) charQueue.dequeue( queueFront )
charStack.pop( stackTop ) if( queueFront
! stackTop ) match false
14ADT Queue Linked-List Implementation
- typedef int QueueItemType // items in Queue
are ints - struct QueueNode // nodes in Queue are
structs - class Queue
-
- public
- // declarations of public member
functions - private
- QueueNode backPtr // pointer to back
of Queue -
- struct QueueNode // place this in
Implementation File - // declaration of QueueNode for
- QueueItemType item // circular, linked
list - QueueNode next
15ADT Queue Member Function Definitions
- // default constructor, which creates a new empty
Queue, - // initializing backPtr to NULL
- QueueQueue( ) backPtr( NULL )
16Queue( const Queue oQueue )
17Queue( const Queue oQueue )
18ADT Queue Member Function Definitions
- // copy constructor, which copies oQueue to a new
Queue - QueueQueue( const Queue oQueue )
- if( oQueue.backPtr NULL )
- backPtr NULL
- else
- // copy node at back of Queue
- backPtr new QueueNode
- assert( backPtr ! NULL )
- backPtr -gt item oQueue.backPtr -gt
item - // copy rest of Queue
-
19ADT Queue Member Function Definitions
- // copy rest of Queue
- QueueNode pnew backPtr
- for( QueueNode porig oQueue.backPtr -gt
next - porig ! oQueue.backPtr
porig porig -gt next ) - pnew -gt next new QueueNode
- assert( pnew -gt next ! NULL )
- pnew pnew -gt next
- pnew -gt item porig -gt item
-
- pnew -gt next backPtr
20ADT Queue Member Function Definitions
- // destructor removes items from Queue until
empty - QueueQueue( )
-
- while( dequeue( ) )
-
- // returns true if Queue is empty, otherwise
returns false - bool QueueisEmpty( ) const
-
- return backPtr NULL
21enqueue( QueueItemType newItem )
22ADT Queue Member Function Definitions
- // adds newItem at the back of a Queue. Returns
true if - // enqueue succeeds, otherwise returns false.
- bool Queueenqueue( QueueItemType newItem )
- QueueNode pnew new QueueNode
- if( pnew NULL ) return false
- pnew -gt item newItem
- if( isEmpty( ) ) pnew -gt next pnew
- else
- pnew -gt next backPtr -gt next
- backPtr -gt next pnew
- backPtr pnew
- return true
23dequeue( )
24ADT Queue Member Function Definitions
- // removes the item at the front of a Queue.
Returns true if - // dequeue succeeds, otherwise returns false.
- bool Queuedequeue( )
-
- if( isEmpty( ) ) return false
- QueueNode frontPtr backPtr -gt next
- if( frontPtr backPtr ) backPtr NULL
- else backPtr -gt next frontPtr -gt
next - frontPtr -gt next NULL
- delete frontPtr
- return true
25ADT Queue Member Function Definitions
- // removes the item at the front of a Queue and
returns it in - // queueFront. Returns true if dequeue
succeeds, otherwise - // returns false.
- bool Queuedequeue( QueueItemType queueFront
) -
- if( isEmpty( ) ) return false
- queueFront (backPtr -gt next) -gt item
- return dequeue( )
26ADT Queue Member Function Definitions
- // retrieves a copy of the item at the front of a
Queue and returns - // it in queueFront, leaving Queue unchanged.
Returns true if - // dequeue succeeds, otherwise returns false.
- bool QueuegetFront( QueueItemType queueFront
) const -
- if( isEmpty( ) ) return false
- queueFront (backPtr -gt next) -gt item
- return true
27ADT Queue Array Implementation
- Suppose that we chose the following, array
implementation for our ADT Queue.
- As usual, additions occur at the back of the
queue, and deletions occur at the front.
28ADT Queue Array Implementation
- Here is what the array will look like after 4
deletions.
- Although the array is nearly empty, only two
positions (6 and 7) are available for new
additions, since additions occur only at the
back. This is called rightward drift.
29ADT Queue Array Implementation
- The problem of rightward drift can be solved by
using the vacant positions at the front of the
array when the positions at the back are filled.
One way of representing this in a diagram is as
follows.
30ADT Queue Array Implementation
- Now, after 4 deletions we have
- And, after 4 subsequent additions we have
31ADT Queue Array Implementation
- When the array is full we have
- If we subsequently make 8 deletions we have
987
1597
32ADT Queue Array Implementation
- Consequently, we need a way to distinguish
between an empty array and a full array - One way to solve this is to keep a count of the
number of items in the array. - Another way is to keep a flag that indicates
whether or not the array is full. (1 bit is
sufficient.) - A third way is to reserve the the position
immediately before the front of the array so that
no data is stored there. (See Walls Mirrors,
chapter 7 for details.) - We will keep a count of the number of items in
the array.
33ADT Queue Array Implementation
- We also need a convenient way to find the next
position in the circular array. - If the array has space for MaxQueue items, then
the - position after position pos is given by
- (pos 1) MaxQueue
- where x y gives the remainder when x is
divided by y - (modulus operator).
- Examples (assume MaxQueue 8)
- (0 1) 8 1 (6 1) 8 7
- (1 1) 8 2 (7 1) 8 0
34ADT Queue Array Implementation
- const int MaxQueue 20 // maximum Queue size
- typedef int QueueItemType // items in Queue
are ints - class Queue
- public
- // declarations of public member
functions - same as for - // linked list
- private
- QueueItemType items MaxQueue
- int front // index to front of items
- int back // index to back of items
- int count // number of elements in
items
35ADT Queue Member Function Definitions
- // default constructor, which creates a new empty
Queue, - // initializing front, back, and count
- QueueQueue( )
- front( 0 ), back( MaxQueue 1 ), count( 0 )
36ADT Queue Member Function Definitions
- // copy constructor, which copies oQueue to a new
Queue - QueueQueue( const Queue oQueue )
- front( oQueue.front ), back( oQueue.back ),
- count( oQueue.count )
-
- int pos oQueue.front
- for( int i 0 i lt oQueue.count i )
-
- items pos oQueue.items pos
- pos (pos 1) MaxQueue
-
37ADT Queue Member Function Definitions
- // destructor - invokes the destructor of static
arrays, - // supplied by the compiler
- QueueQueue( )
- // returns true if the Queue is empty, otherwise
returns false - bool QueueisEmpty( ) const
-
- return count 0
-
38ADT Queue Member Function Definitions
- // adds newItem at the back of a Queue. Returns
true if - // enqueue succeeds, otherwise returns false.
- bool Queueenqueue( QueueItemType newItem )
-
- if( count MaxQueue ) return false
- back (back 1) MaxQueue
- items back newItem
- count
- return true
39ADT Queue Member Function Definitions
- // removes the item at the front of a Queue.
Returns true if - // dequeue succeeds, otherwise returns false.
- bool Queuedequeue( )
-
- if( isEmpty( ) ) return false
- front (front 1) MaxQueue
- count
- return true
40ADT Queue Member Function Definitions
- // removes the item at the front of a Queue and
returns it in - // queueFront. Returns true if dequeue
succeeds, otherwise - // returns false.
- bool Queuedequeue( QueueItemType queueFront
) -
- if( isEmpty( ) ) return false
- queueFront items front
- front (front 1) MaxQueue
- count
- return true
41ADT Queue Member Function Definitions
- // retrieves a copy of the item at the front of a
Queue and returns - // it in queueFront, leaving Queue unchanged.
Returns true if - // dequeue succeeds, otherwise returns false.
- bool QueuegetFront( QueueItemType queueFront
) const -
- if( isEmpty( ) ) return false
- queueFront items front
- return true
42Queueing Simulation
- Read this case study in Chapter 7 of your text,
Walls Mirrors. - This is a good illustration of how queues in a
computer program can be used to predict the
performance of queues in real life. Key concepts
include - simulated vs. real time
- time-driven vs. event-driven simulation
- the use of event lists in event-driven simulations