Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.

Similar presentations


Presentation on theme: "What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the."— Presentation transcript:

1 What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). n A queue is a “first in, first out” or FIFO structure.

2 Queue ADT Operations n makeEmpty -- Sets queue to an empty state. n isEmpty -- Determines whether the queue is currently empty. n isFull -- Determines whether the queue is currently full. n enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. n dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 2

3 DYNAMIC ARRAY IMPLEMENTATION QueType ~QueType enqueue dequeue. class QueType Private Data: front 1 rear 4 maxQue 5 items ‘C’ ‘X’ ‘J’ items [0] [1] [2] [3] [4] RESERVED

4 charletter = ‘V’; QueType charQue; charQue.enqueue(letter); charQue.enqueue(‘C’); charQue.enqueue(‘S’); if ( !charQue.isEmpty( )) charQue.dequeue(letter); charQueue.enqueue(‘K’); Tracing Client Code Private data: front rear [0] [1] [2] [3] [4] 4 letter ‘V’ 4

5 charletter = ‘V’; QueType charQue; charQue.enqueue(letter); charQue.enqueue(‘C’); charQue.enqueue(‘S’); if ( !charQue.isEmpty( )) charQue.dequeue(letter); charQueue.enqueue(‘K’); Tracing Client Code Private data: front rear [0] [1] [2] [3] [4] 0 C S K letter ‘V’ 3

6 // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); QueType( int max );// PARAMETERIZED CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR bool isEmpty ( ) const; bool isFull( ) const; void enqueue( ItemType item ); void dequeue( ItemType& item ); private: int front; int rear; int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 6

7 // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d template QueType ::QueType( int max ) // PARAMETERIZED { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates } template bool QueType ::isEmpty( ) { return ( rear == front ) } 7

8 template QueType ::~QueType( ) { delete [ ] items; // deallocates array }. template bool QueType ::isFull( ) {// WRAP AROUND return ( (rear + 1) % maxQue == front ) } 8

9 template QueType ::enqueue(ItemType newItem ) { rear = (rear+1) % maxQue; items[rear] = newItem; }. template bool QueType ::dequeue(ItemType & newItem ) { front = (front+1) % maxQue; item = items[front]; } 9


Download ppt "What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the."

Similar presentations


Ads by Google