Queues

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).

A queue is a FIFO “first in, first out” structure.

Operations:    

MakeEmpty -- Sets queue to an empty state.

IsEmpty -- Determines whether the queue is currently empty.

IsFull -- Determines whether the queue is currently full.

Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue.

Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item.

 

Implementation (using dynamic array)

 

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


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

template
QueType::~QueType( )
{
	delete [ ] items; 	// deallocates array
}

 . 
 . 
 .

template
bool QueType::IsFull( )

{							// WRAP AROUND
	return ( (rear + 1) % maxQue == front )
}

An example......

 

© Lynne Grewe