1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a...

42
1 Queues (Walls & Mirrors - Chapter 7)
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    3

Transcript of 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a...

Page 1: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

1

Queues

(Walls & Mirrors - Chapter 7)

Page 2: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

2

Overview

• The ADT Queue

• Linked-List Implementation of a Queue

• Array Implementation of a Queue

Page 3: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

3

The 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.

OUT IN

(back)(front)

Page 4: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

4

The ADT Queue (Cont’d.)

• 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 don’t need to be concerned with whether the queue is implemented as an array, a linked list, or something else.

Page 5: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

5

Comparing 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.

Page 6: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

6

ADT 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.

Page 7: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

7

ADT 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.

Page 8: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

8

ADT 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.

Page 9: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

9

Application: 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.

Page 10: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

10

Recognizing Palindromes:“RADAR”

CurrentCharacter

Queue(back at right)

Stack(top at left)

R R R

A R A A R

D R A D D A R

A R A D A A D A R

R R A D A R R A D A R

Step 1: Copy “RADAR” into Queue and Stack:

Page 11: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

11

Recognizing Palindromes:“RADAR”

Queue(front at left)

Front ofQueue

Top ofStack

Stack(top at left)

R A D A R R R R A D A R

A D A R A A A D A R

D A R D D D A R

A R A A A R

R R R R

empty empty empty empty

Step 2: Remove “RADAR” from Queue and Stack:

Page 12: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

12

Recognizing 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;

}

Page 13: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

13

Recognizing Palindromes// copy each character of str[ ] into both charQueue and charStack

for( int i = 0; i < strlen( str ) – 1; i++ )

{ charQueue.enqueue( str[i] );

charStack.push( str[i] ); }

// 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; }

Page 14: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

14

ADT Queue: Linked-List Implementationtypedef int QueueItemType; // items in Queue are int’sstruct QueueNode; // nodes in Queue are struct’s

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;};

Page 15: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

15

ADT Queue: Member Function Definitions

// default constructor, which creates a new empty Queue,// initializing backPtr to NULL

Queue::Queue( ) : backPtr( NULL ) { }

Page 16: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

16

Queue( const Queue &oQueue )

5 8 13

oQueue.backPtr

Page 17: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

17

Queue( const Queue &oQueue )

backPtr

13

pnew

5

5 8 13

oQueue.backPtrporig

Page 18: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

18

ADT Queue: Member Function Definitions

// copy constructor, which copies oQueue to a new Queue

Queue::Queue( const Queue &oQueue )

{ if( oQueue.backPtr = = NULL ) backPtr = NULL; else

{ // copy node at back of Queue

backPtr = new QueueNode;

assert( backPtr != NULL );

backPtr -> item = oQueue.backPtr -> item;

// copy rest of Queue }}

Page 19: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

19

ADT Queue: Member Function Definitions

// copy rest of Queue

QueueNode *pnew = backPtr;

for( QueueNode *porig = oQueue.backPtr -> next;

porig != oQueue.backPtr ; porig = porig -> next )

{ pnew -> next = new QueueNode;

assert( pnew -> next != NULL );

pnew = pnew -> next;

pnew -> item = porig -> item;

}

pnew -> next = backPtr;

Page 20: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

20

ADT Queue: Member Function Definitions

// destructor removes items from Queue until empty

Queue::~Queue( ){ while( dequeue( ) );}

// returns true if Queue is empty, otherwise returns false

bool Queue::isEmpty( ) const{ return backPtr = = NULL;}

Page 21: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

21

enqueue( QueueItemType newItem )

5 8 13

pnewnewItem 21

21

backPtr

pnew

21

backPtr

Page 22: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

22

ADT Queue: Member Function Definitions// adds newItem at the back of a Queue. Returns true if// enqueue succeeds, otherwise returns false.

bool Queue::enqueue( QueueItemType newItem )

{ QueueNode *pnew = new QueueNode;

if( pnew = = NULL ) return false;

pnew -> item = newItem;

if( isEmpty( ) ) pnew -> next = pnew;

else

{ pnew -> next = backPtr -> next;

backPtr -> next = pnew; }

backPtr = pnew;

return true;

}

Page 23: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

23

dequeue( )

21

frontPtr backPtr

8 13 21

backPtrfrontPtr

5

Page 24: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

24

ADT Queue: Member Function Definitions

// removes the item at the front of a Queue. Returns true if// dequeue succeeds, otherwise returns false.

bool Queue::dequeue( )

{

if( isEmpty( ) ) return false;

QueueNode *frontPtr = backPtr -> next;

if( frontPtr = = backPtr ) backPtr = NULL;

else backPtr -> next = frontPtr -> next;

frontPtr -> next = NULL;

delete frontPtr;

return true;

}

Page 25: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

25

ADT 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 Queue::dequeue( QueueItemType &queueFront )

{

if( isEmpty( ) ) return false;

queueFront = (backPtr -> next) -> item;

return dequeue( );

}

Page 26: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

26

ADT 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 Queue::getFront( QueueItemType &queueFront ) const

{

if( isEmpty( ) ) return false;

queueFront = (backPtr -> next) -> item;

return true;

}

Page 27: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

27

ADT Queue: Array Implementation

• Suppose that we chose the following, array implementation for our ADT Queue.

89553421138

0 1 2 3 4 5 6 7

front 0 back 5

• As usual, additions occur at the back of the queue, and deletions occur at the front.

Page 28: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

28

ADT Queue: Array Implementation

• Here is what the array will look like after 4 deletions.

8955

0 1 2 3 4 5 6 7

front 4 back 5

• 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.

Page 29: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

29

ADT 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.

8

13

21

3455

89

0

1

2

34

5

6

7

0 front

5back

Page 30: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

30

ADT Queue: Array Implementation

Now, after 4 deletions we have:

And, after 4 subsequent additions we have:

55

89

0

1

2

34

5

6

7

4 front 5back

55

89

0

1

2

34

5

6

7

4 front 1back

144

233 377

610

Page 31: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

31

ADT Queue: Array Implementation

When the array is full we have:

If we subsequently make 8 deletions we have:

0

1

2

34

5

6

7

4 front 3back

55

89

0

1

2

34

5

6

7

4 front 3back

144

233 377

610

987

1597

Page 32: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

32

• 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.

ADT Queue: Array Implementation

Page 33: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

33

We also need a convenient way to find the next position in the circular array.

• If the array has space for MaxQueue items, then theposition 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

ADT Queue: Array Implementation

Page 34: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

34

ADT Queue: Array Implementationconst int MaxQueue = 20; // maximum Queue size

typedef int QueueItemType; // items in Queue are int’s

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[ ]

};

Page 35: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

35

ADT Queue: Member Function Definitions

// default constructor, which creates a new empty Queue,// initializing front, back, and count

Queue::Queue( ) :

front( 0 ), back( MaxQueue – 1 ), count( 0 ) { }

Page 36: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

36

ADT Queue: Member Function Definitions

// copy constructor, which copies oQueue to a new Queue

Queue::Queue( const Queue &oQueue ) :front( oQueue.front ), back( oQueue.back ),count( oQueue.count )

{ int pos = oQueue.front; for( int i = 0; i < oQueue.count; i++ ) { items[ pos ] = oQueue.items[ pos ]; pos = (pos + 1) % MaxQueue; }}

Page 37: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

37

ADT Queue: Member Function Definitions

// destructor - invokes the destructor of static arrays,// supplied by the compiler

Queue::~Queue( ) { }

// returns true if the Queue is empty, otherwise returns false

bool Queue::isEmpty( ) const{ return count = = 0;}

Page 38: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

38

ADT Queue: Member Function Definitions

// adds newItem at the back of a Queue. Returns true if// enqueue succeeds, otherwise returns false.

bool Queue::enqueue( QueueItemType newItem )

{

if( count = = MaxQueue ) return false;

back = (back + 1) % MaxQueue;

items[ back ] = newItem;

count ++;

return true;

}

Page 39: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

39

ADT Queue: Member Function Definitions

// removes the item at the front of a Queue. Returns true if// dequeue succeeds, otherwise returns false.

bool Queue::dequeue( )

{

if( isEmpty( ) ) return false;

front = (front + 1) % MaxQueue;

count – –;

return true;

}

Page 40: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

40

ADT 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 Queue::dequeue( QueueItemType &queueFront )

{

if( isEmpty( ) ) return false;

queueFront = items[ front ];

front = (front + 1) % MaxQueue;

count – –;

return true;

}

Page 41: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

41

ADT 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 Queue::getFront( QueueItemType &queueFront ) const

{

if( isEmpty( ) ) return false;

queueFront = items[ front ];

return true;

}

Page 42: 1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.

42

Queueing 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