QueueNode.h
template<typename Type, typename Cmp> class PriorityQueue; template<typename Type, typename Cmp> class QueueNode { private: friend class PriorityQueue<Type, Cmp>; QueueNode(Type item, QueueNode<Type, Cmp> *next=NULL): data(item), pnext(next){} private: Type data; QueueNode<Type, Cmp> *pnext; };
Compare.h
template<typename Type> class Compare { public: static bool lt(Type item1, Type item2); }; template<typename Type> bool Compare<Type>::lt(Type item1, Type item2) { return item1 < item2; } struct SpecialData { friend ostream& operator<<(ostream& , SpecialData &); friend bool operator<=(SpecialData item1, SpecialData item2); int tenor; int pir; }; ostream& operator<<(ostream &os, SpecialData &out) { os<<out.tenor<<" "<<out.pir; return os; } inline bool operator<=(SpecialData item1, SpecialData item2) { return item1.pir <= item2.pir; } class SpecialCmp { public: static bool lt(SpecialData item1, SpecialData item2); }; bool SpecialCmp::lt(SpecialData item1, SpecialData item2) { return item1.pir < item2.pir; }
PriorityQueue.h
#include "QueueNode.h" #include "Compare.h" template<typename Type, typename Cmp> class PriorityQueue { public: PriorityQueue() : prear(NULL), pfront(NULL){} ~PriorityQueue() { MakeEmpty(); } public: void MakeEmpty(); void Append(const Type item); Type Delete(); Type GetFront(); void Print(); bool IsEmpty() const { return prear == NULL; } private: QueueNode<Type, Cmp> *prear, *pfront; }; template<typename Type, typename Cmp> void PriorityQueue<Type, Cmp>::MakeEmpty() { QueueNode<Type, Cmp> *pdel; while(pfront) { pdel = pfront; pfront = pfront->pnext; delete pdel; } } template<typename Type, typename Cmp> void PriorityQueue<Type, Cmp>::Append(const Type item) { if(pfront == NULL) { pfront = prear = new QueueNode<Type, Cmp>(item); } else { prear = prear->pnext = new QueueNode<Type, Cmp>(item); } } template<typename Type, typename Cmp> Type PriorityQueue<Type, Cmp>::Delete() { if(pfront == NULL) { cout<<"the queue has been empty"; exit(1); } //如果只有一个节点 if(pfront == prear) { Type item = pfront->data; delete pfront; pfront = NULL; return item; } QueueNode<Type, Cmp> *pdel,*pmov; pdel = pfront; pmov = pfront; //寻找从第二个节点开始的最小优先级的结点,注pdel指向的是最小优先级节点的前一个节点 while(pmov->pnext) { if( Cmp::lt(pmov->pnext->data, pdel->pnext->data) ) pdel = pmov; pmov = pmov->pnext; } //如果第一个节点是最小优先级节点 if( pfront->data <= pdel->pnext->data ) { pdel = pfront; Type item = pdel->data; pfront = pfront->pnext; delete pdel; return item; } else { pmov = pdel; pdel= pdel->pnext; pmov->pnext = pdel->pnext; Type item = pdel->data; delete pdel; return item; } } template<typename Type, typename Cmp> Type PriorityQueue<Type, Cmp>::GetFront() { if(pfront == NULL) { cout<<"the queue is empty"<<endl; exit(1); } QueueNode<Type, Cmp> *pmov=pfront->pnext, *p = pfront; while(pmov) { if( Cmp::lt(pmov->data, p->data) ) p = pmov; pmov = pmov->pnext; } return p->data; } template<typename Type, typename Cmp> void PriorityQueue<Type, Cmp>::Print() { cout<<"front"; QueueNode<Type,Cmp> *pmov = pfront; while(pmov) { cout<<"-->"<<pmov->data; pmov = pmov->pnext; } cout<<"-->rear"<<endl<<endl; }