1 / 42

Queues

Queues. (Walls & Mirrors - Chapter 7). Overview. The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue. OUT. IN. (front). (back). The ADT Queue.

clio
Download Presentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues (Walls & Mirrors - Chapter 7)

  2. Overview • The ADT Queue • Linked-List Implementation of a Queue • Array Implementation of a Queue

  3. OUT IN (front) (back) 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.

  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.

  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.

  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.

  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.

  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.

  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.

  10. Recognizing Palindromes:“RADAR” Step 1: Copy “RADAR” into Queue and Stack:

  11. Recognizing Palindromes:“RADAR” Step 2: Remove “RADAR” from Queue and Stack:

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

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

  14. ADT Queue: Linked-List Implementation typedef int QueueItemType; // items in Queue are int’s struct 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; };

  15. ADT Queue: Member Function Definitions // default constructor, which creates a new empty Queue, // initializing backPtr to NULL Queue::Queue( ) : backPtr( NULL ) { }

  16. 5 8 13 oQueue.backPtr Queue( const Queue &oQueue )

  17. 5 8 13 porig oQueue.backPtr 5 13 pnew backPtr Queue( const Queue &oQueue )

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

  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;

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

  21. 21 5 8 13 21 newItem backPtr pnew 21 backPtr pnew enqueue( QueueItemType newItem )

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

  23. 21 5 8 13 frontPtr backPtr 21 backPtr frontPtr dequeue( )

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

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

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

  27. 0 1 2 3 4 5 6 7 8 13 21 34 55 89 0 5 front back ADT 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.

  28. 0 1 2 3 4 5 6 7 55 89 4 5 front back ADT 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.

  29. 7 0 0 front 8 1 6 5 13 back 89 21 2 5 55 34 3 4 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.

  30. Now, after 4 deletions we have: And, after 4 subsequent additions we have: 233 377 7 7 0 0 4 4 144 610 front front 1 1 6 6 5 1 back back 89 89 2 2 5 5 55 55 3 3 4 4 ADT Queue: Array Implementation

  31. When the array is full we have: If we subsequently make 8 deletions we have: 233 377 7 7 0 0 4 4 144 610 front front 1 1 6 6 3 3 back back 89 2 2 5 5 55 3 3 4 4 ADT Queue: Array Implementation 987 1597

  32. ADT 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. (1bit 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.

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

  34. ADT Queue: Array Implementation const 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[ ] };

  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 ) { }

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

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

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

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

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

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

  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

More Related