Queue with Dynamic Linked Structure

 

We dynamically allocate the space for each queue element as it is placed into queue (FIFIO)

Char

Code Implementation


#include "ItemType.h"      // for ItemType 		

template<ItemType>
class QueType  {
public:
	QueType( );    		// CONSTRUCTOR
	~QueType( ) ;		// DESTRUCTOR
	bool IsEmpty( ) const;	
	bool IsFull( ) const;
	void Enqueue( ItemType item );
	void Dequeue( ItemType&  item );
	void MakeEmpty( );
private:
	NodeType*  qFront;
	NodeType*  qRear;
};

           



template<class ItemType>
QueType<ItemType>::QueType( )  	// CONSTRUCTOR
{
	qFront = NULL;
	qRear = NULL;
}

template <class ItemType>
bool QueType<ItemType>::IsEmpty( ) const
{						
	return ( qFront == NULL )
}

template <class ItemType>
void QueType<ItemType>::Enqueue( ItemType newItem )
		// Adds newItem to the rear of the queue.
		// Pre:  Queue has been initialized.
		//	  Queue is not full.
		// Post: newItem is at rear of queue.
{
	NodeType<ItemType>*  ptr;

	ptr = new  NodeType<ItemType>;
	ptr->info = newItem;
	ptr->next = NULL;
	if  ( qRear == NULL )
		qFront = ptr;
	else
		qRear->next = ptr;
	qRear = ptr;
}

template <class ItemType>
void QueType<ItemType>::Dequeue( ItemType& item )
		// Removes element from from front of queue
		// and returns it in item.
		// Pre:  Queue has been initialized.
		//	  Queue is not empty.
		// Post: Front element has been removed from queue.
		// 	   item is a copy of removed element.
{	
	NodeType<ItemType>*  tempPtr;

	tempPtr = qFront;
	item = qFront->info;
	qFront = qFornt->next;
	if  ( qFront == NULL )
		qRear = NULL;
	delete  tempPtr;
}


 

 

© Lynne Grewe