Sessions 26 and 27 - Data Structures & Algorithm

end function enqueue dequeue calls list function

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 49 Queues Upcoming program Queue implementation Reuse List as before insertAtBack (enqueue) removeFromFront (dequeue) Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 50 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 16 17 18 19 20 21 22 23 24 25 // Template Queue class definition derived from class List. #ifndef QUEUE_H #define QUEUE_H Inherit from template class List. #include "list.h" // List class definition template< class QUEUETYPE > class Queue : private List< QUEUETYPE > { public: // enqueue calls List function insertAtBack void enqueue( const QUEUETYPE &data ) { insertAtBack( data ); Reuse the appropriate List functions. } // end function enqueue // dequeue calls List function removeFromFront bool dequeue( QUEUETYPE &data ) { return removeFromFront( data ); } // end function dequeue Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 51 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 // isQueueEmpty calls List function isEmpty bool isQueueEmpty() const { return isEmpty(); } // end function isQueueEmpty // printQueue calls List function print void printQueue() const { print(); } // end function printQueue }; // end class Queue #endif Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 52 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 16 17 18 19 20 21 22 23 24 // Template Queue class test program. #include <iostream> using std::endl; #include "queue.h" // Queue class definition int main() { Queue< int > intQueue; // create Queue of ints cout << "processing an integer Queue" << endl; // enqueue integers onto intQueue for ( int i = 0; i < 4; i++ ) { intQueue.enqueue( i ); intQueue.printQueue(); } // end for // dequeue integers from intQueue int dequeueInteger; Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 53 25 26 27 28 29 30 31 32 33 34 35 36 37 37 38 39 40 41 42 43 44 while ( !intQueue.isQueueEmpty() ) { intQueue.dequeue( dequeueInteger ); cout << dequeueInteger << " dequeued" << endl; intQueue.printQueue(); } // end while Queue< double > doubleQueue; // create Queue of doubles double value = 1.1; cout << "processing a double Queue" << endl; // enqueue floating-point values onto doubleQueue for ( int j = 0; j< 4; j++ ) { doubleQueue.enqueue( value ); doubleQueue.printQueue(); value += 1.1; } // end for Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 54 45 46 47 48 49 50 51 52 53 53 54 55 56 57 // dequeue floating-point values from doubleQueue double dequeueDouble; while ( !doubleQueue.isQueueEmpty() ) { doubleQueue.dequeue( dequeueDouble ); cout << dequeueDouble << " dequeued" << endl; doubleQueue.printQueue(); } // end while return 0; } // end main Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 55 processing an integer Queue The list is: 0 The list is: 0 1 The list is: 0 1 2 The list is: 0 1 2 3 0 dequeued The list is: 1 2 3 1 dequeued The list is: 2 3 2 dequeued The list is: 3 3 dequeued The list is emp...
View Full Document

Ask a homework question - tutors are online