//Header file linkedQueue.h #ifndef H_linkedQueue #define H_linkedQueue #include #include #include "queueADT.h" using namespace std; //************************************************************* // Author: D.S. Malik // // This class specifies the basic operations on a queue as a // linked list. //************************************************************* template class linkedQueueType: public queueADT { public: const linkedQueueType& operator= (const linkedQueueType&); //Overload the assignment operator. bool isEmptyQueue() const; //Function to determine whether the queue is empty. //Postcondition: Returns true if the queue is empty, // otherwise returns false. bool isFullQueue() const; //Function to determine whether the queue is full. //Postcondition: Returns true if the queue is full, // otherwise returns false. void initializeQueue(); //Function to initialize the queue to an empty state. //Postcondition: queueFront = NULL; queueRear = NULL Type front() const; //Function to return the first element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: If the queue is empty, the program // terminates; otherwise, the first element of the // queue is returned. Type back() const; //Function to return the last element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: If the queue is empty, the program // terminates; otherwise, the last element of the // queue is returned. void addQueue(const Type& queueElement); //Function to add queueElement to the queue. //Precondition: The queue exists and is not full. //Postcondition: The queue is changed and queueElement is // added to the queue. void deleteQueue(); //Function to remove the first element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: The queue is changed and the first element // is removed from the queue. linkedQueueType(); //Default constructor linkedQueueType(const linkedQueueType& otherQueue); //Copy constructor ~linkedQueueType(); //Destructor private: nodeType *queueFront; //pointer to the front of the queue nodeType *queueRear; //pointer to the rear of the queue }; //Default constructor template linkedQueueType::linkedQueueType() { queueFront = NULL; //set front to null queueRear = NULL; //set rear to null } //end default constructor template bool linkedQueueType::isEmptyQueue() const { return(queueFront == NULL); } //end template bool linkedQueueType::isFullQueue() const { return false; } //end isFullQueue template void linkedQueueType::initializeQueue() { nodeType *temp; while (queueFront!= NULL) //while there are elements left //in the queue { temp = queueFront; //set temp to point to the //current node queueFront = queueFront->link; //advance first to //the next node delete temp; //deallocate memory occupied by temp } queueRear = NULL; //set rear to NULL } //end initializeQueue template void linkedQueueType::addQueue(const Type& newElement) { nodeType *newNode; newNode = new nodeType; //create the node newNode->info = newElement; //store the info newNode->link = NULL; //initialize the link field to NULL if (queueFront == NULL) //if initially the queue is empty { queueFront = newNode; queueRear = newNode; } else //add newNode at the end { queueRear->link = newNode; queueRear = queueRear->link; } }//end addQueue template Type linkedQueueType::front() const { assert(queueFront != NULL); return queueFront->info; } //end front template Type linkedQueueType::back() const { assert(queueRear!= NULL); return queueRear->info; } //end back template void linkedQueueType::deleteQueue() { nodeType *temp; if (!isEmptyQueue()) { temp = queueFront; //make temp point to the //first node queueFront = queueFront->link; //advance queueFront delete temp; //delete the first node if (queueFront == NULL) //if after deletion the //queue is empty queueRear = NULL; //set queueRear to NULL } else cout << "Cannot remove from an empty queue" << endl; }//end deleteQueue //Destructor template linkedQueueType::~linkedQueueType() { //Write the definition of the destructor } //end destructor template const linkedQueueType& linkedQueueType::operator= (const linkedQueueType& otherQueue) { //Write the definition of to overload the assignment operator } //end assignment operator //copy constructor template linkedQueueType::linkedQueueType (const linkedQueueType& otherQueue) { //Write the definition of the copy constructor }//end copy constructor #endif