// PriorityQueue.h Dave Reed 9/10/02 // // A templated PriorityQueue class, implemented using linked lists. //////////////////////////////////////////////////////////////////// #ifndef _PRIORITY_QUEUE_ #define _PRIORITY_QUEUE_ template class PriorityNode // class for storing some item { // in a linked list with priority public: Type item; int priority; PriorityNode * next; }; template class PriorityQueue { public: PriorityQueue(); // constructor PriorityQueue(const PriorityQueue & q); // copy constructor ~PriorityQueue(); // destructor PriorityQueue & operator=(const PriorityQueue & q); bool IsEmpty() const; // returns if queue is empty void Enter(const Type & item, int pri); // enters item in queue void Remove(); // removes item at front of queue Type & Front(); // returns item at front of queue int FrontPriority(); // returns priority of front item private: PriorityNode * myFront; // pointers to linked list of items PriorityNode * CopyList(const PriorityNode * ptr); }; ////////////////////////////////////////////////////////////////////////// template PriorityQueue::PriorityQueue() // Constructor: initializes queue to be empty (myFront = myRear = NULL) { myFront = NULL; } template PriorityNode * PriorityQueue::CopyList(const PriorityNode * ptr) // private member function to recursively copy a linked list { if (ptr == NULL) { return NULL; } else { PriorityNode * copyFront = new PriorityNode; copyFront->item = ptr->item; copyFront->priority = ptr->priority; copyFront->next = CopyList(ptr->next); return copyFront; } } template PriorityQueue::PriorityQueue(const PriorityQueue & q) // Constructor: initializes queue to be empty (myFront = myRear = NULL) { myFront = CopyList(q.myFront); } template PriorityQueue::~PriorityQueue() // Destructor: deallocates nodes in the linked list { while (!IsEmpty()) { Remove(); } } template PriorityQueue & PriorityQueue::operator=(const PriorityQueue & q) // Assignment operator { if (this != &q) { while (!IsEmpty()) { Remove(); } myFront = CopyList(q.myFront); } return *this; } template bool PriorityQueue::IsEmpty() const // Returns: true if empty queue, else false { return (myFront == NULL); } template void PriorityQueue::Enter(const Type & toBeEntered, int pri) // Results: enters toBeEntered at rear of queue { PriorityNode * temp = new PriorityNode; temp->item = toBeEntered; temp->priority = pri; temp->next = NULL; if (myFront == NULL || temp->priority < myFront->priority) { temp->next = myFront; myFront = temp; } else { PriorityNode * trailer = myFront; while (trailer->next != NULL && trailer->next->priority <= temp->priority) { trailer = trailer->next; } temp->next = trailer->next; trailer->next = temp; } } template void PriorityQueue::Remove() // Assumes: queue is not empty // Results: removes an item from the front of the queue { PriorityNode * temp = myFront; myFront = myFront->next; delete temp; } template Type & PriorityQueue::Front() // Assumes: queue is not empty // Returns: reference to item at the front of the queue { return myFront->item; } template int PriorityQueue::FrontPriority() // Assumes: queue is not empty // Returns: priority of item at the front of the queue { return myFront->priority; } #endif