Queue - CSS342: Queues Professor: Munehiro Fukuda CSS342:...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CSS342: Queues 1 CSS342: Queues Professor: Munehiro Fukuda
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Queues 2 Topics Basic concepts of queue Queue implementation Queues used in Applications Operating Systems Network
Background image of page 2
CSS342: Queues 3 Concepts of Queue Queue of Bank Customers First-In First-Out (FIFO) service How can you estimate the average wait? Solution: Simulate it using queues Time = 0 Time = 12 Time = 20 Time = 38 Basic Concepts
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Queues 4 Queue Specification class Queue { public: Queue( ); Queue( const Queue& Q ); ~Queue( ); bool isEmpty( ) const; bool enqueue( const Object &newItem ); Object dequeue( ); Object getFront( ) const; } retrieve the front item but do not remove it Unlike stacks: (aQueue.enqueue(newItem)).dequeue() is Not equal to aQueue unless it is empty. dequeue( ): remove and get the front item enqueue to the back Basic Concepts
Background image of page 4
CSS342: Queues 5 STL Class queue #include <queue> queue( ); Creates an empty queue. You can convert a list into a new stack. See your text pages 351 and 353 Example: queue<int> aQueue; bool empty( ) const; Returns if the queue is empty. Example: while ( aQueue.empty( ) == false ) { … } size_type size( ) const; Returns the number of elements. Size_type is an integral type. You can compare it with an integer. Example: while (aQueue.size( ) > 0) { … } T &front( ); Returns a reference to the first element. Example: while( !aQueue.empty() ) { cout << aQueue.front() << endl; aQueue.pop(); } T &back( ); Returns a reference to the last element. Example: if( !aQueue.empty() ) cout << aQueue.front() << endl; void pop( ); Removes the first element from the queue. Example: see the front( ) example void push( const T& value ); Inserts an item with value at the back of the queue. Example: stack<int> aQueue; for( int i=0; i < 10; i++ ) aQueue.push(i); Basic Concepts
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Queues 6 STL Class queue Example #include <queue> #include <iostream> using namespace std; int main( ) { queue<int> aQueue; int item; for ( int j = 0; j < 5; j++ ) aQueue.push( j ); while ( !aQueue.empty( ) ) { cout << aQueue.front( ) << endl; aQueue.pop( ); } } Results: 0 1 2 3 4 Basic Concepts
Background image of page 6
CSS342: Queues 7 Queue Implementation We learned how to use the queue. Then how can we implement it? Array-based implementation Pointer-based implementation Reuse of linked lists Implementation
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSS342: Queues 8 An Array-Based Implementation (Naïve Implementation) k ….. 7 ….. 1 4 2 back 0 1 2 k theArray.size( )-1 0 front 49 10 7 ….. back 0 1 2 theArray.size( )-1 47 front 4 47 48 49 template<class Object> class Queue { public: Queue( ); …..; private: vector<Object> theArray; int front; int back; } Problem: rightward drift Shift array elements to the left is costly. Implementation
Background image of page 8
CSS342: Queues 9 An Array-Based Implementation (A Circular Array Implementation) template <class Object> class Queue { public: Queue( ); bool isEmpty( ) const; const Object getfront( ) const; void flush( ); Object dequeue( ); void enqueue( const Object &newItem ); Private: Vector<Object> theArray; int front; int back; int currentSize: // number of items in queue void doubleQueue( ); // make the size of theArray double } 2 4 1 7 0 1 2 3 theArray.size( ) -1 front back Invariant: front should not pass more than 1 item ahead from back.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 12/20/2009.

Page1 / 40

Queue - CSS342: Queues Professor: Munehiro Fukuda CSS342:...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online