Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue."— Presentation transcript:

1 1 Queues (Walls & Mirrors - Chapter 7)

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

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

4 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 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 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 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 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 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 10 Recognizing Palindromes:“RADAR” Step 1: Copy “RADAR” into Queue and Stack:

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

12 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 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 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 15 ADT Queue: Member Function Definitions // default constructor, which creates a new empty Queue, // initializing backPtr to NULL Queue::Queue( ) : backPtr( NULL ) { }

16 16 Queue( const Queue &oQueue ) 5813 oQueue.backPtr

17 17 Queue( const Queue &oQueue ) backPtr 13 pnew 5 5813 oQueue.backPtrporig

18 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 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 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 enqueue( QueueItemType newItem ) 5813 pnewnewItem 21 backPtr pnew 21 backPtr

22 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 23 dequeue( ) 21 frontPtr backPtr 81321 backPtrfrontPtr 5

24 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 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 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 27 ADT Queue: Array Implementation Suppose that we chose the following, array implementation for our ADT Queue. 89553421138 01234567 front 0 back 5 As usual, additions occur at the back of the queue, and deletions occur at the front.

28 28 ADT Queue: Array Implementation Here is what the array will look like after 4 deletions. 8955 01234567 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.

29 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 3 4 5 6 7 0 front 5 back

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

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

32 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

33 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 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 ADT Queue: Array Implementation

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


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

Similar presentations


Ads by Google