优先级队列
2012-04-08 23:06 Rollen Holt 阅读(1169) 评论(4) 编辑 收藏 举报QueueNode.h
template<typename Type,typename Cmp> class PriorityQueue; template<typename Type,typename Cmp> class QueueNode{ private: friend class PriorityQueue<Type,Cmp>; QueueNode(const Type item,QueueNode<Type,Cmp> *next=NULL) :m_data(item),m_pnext(next){} private: Type m_data; QueueNode<Type,Cmp> *m_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 &); int m_ntenor; int m_npir; }; ostream& operator<<(ostream& os,SpecialData &out){ os<<out.m_ntenor<<" "<<out.m_npir; return os; } class SpecialCmp{ //处理特殊比较大小,用户可添加适当的类 public: static bool lt(SpecialData item1,SpecialData item2); }; bool SpecialCmp::lt(SpecialData item1, SpecialData item2){ return item1.m_npir<item2.m_npir; }
PriorityQueue.h
#include "QueueNode.h" #include "Compare.h" template<typename Type,typename Cmp> class PriorityQueue{ //Cmp is Designed for compare public: PriorityQueue():m_prear(NULL),m_pfront(NULL){} ~PriorityQueue(){ MakeEmpty(); } void MakeEmpty(); //make the queue empty void Append(const Type item); //insert data Type Delete(); //delete data Type GetFront(); //get data void Print(); //print the queue bool IsEmpty() const{ return m_pfront==NULL; } private: QueueNode<Type,Cmp> *m_prear,*m_pfront; }; template<typename Type,typename Cmp> void PriorityQueue<Type,Cmp>::MakeEmpty(){ QueueNode<Type,Cmp> *pdel; while(m_pfront){ pdel=m_pfront; m_pfront=m_pfront->m_pnext; delete pdel; } } template<typename Type,typename Cmp> void PriorityQueue<Type,Cmp>::Append(const Type item){ if(m_pfront==NULL){ m_pfront=m_prear=new QueueNode<Type,Cmp>(item); } else{ m_prear=m_prear->m_pnext=new QueueNode<Type,Cmp>(item); } } template<typename Type,typename Cmp> Type PriorityQueue<Type,Cmp>::Delete(){ if(IsEmpty()){ cout<<"There is no elements!"<<endl; exit(1); } QueueNode<Type,Cmp> *pdel=m_pfront,*pmove=m_pfront; while(pmove->m_pnext){ //get the minimize priority's data //cmp:: lt is used for compare the two data, if the front one // is less than the back, then return 1 if(Cmp::lt(pmove->m_pnext->m_data,pdel->m_pnext->m_data)){ pdel=pmove; } pmove=pmove->m_pnext; } pmove=pdel; pdel=pdel->m_pnext; pmove->m_pnext=pdel->m_pnext; Type temp=pdel->m_data; delete pdel; return temp; } template<typename Type,typename Cmp> Type PriorityQueue<Type,Cmp>::GetFront(){ if(IsEmpty()){ cout<<"There is no elements!"<<endl; exit(1); } QueueNode<Type,Cmp> *pdel=m_pfront,*pmove=m_pfront->m_pnext; while(pmove){ //get the minimize priority's data if(Cmp::lt(pmove->m_data,pdel->m_data)){ pdel=pmove; } pmove=pmove->m_pnext; } return pdel->m_data; } template<typename Type,typename Cmp> void PriorityQueue<Type,Cmp>::Print(){ QueueNode<Type,Cmp> *pmove=m_pfront; cout<<"front"; while(pmove){ cout<<"--->"<<pmove->m_data; pmove=pmove->m_pnext; } cout<<"--->rear"<<endl<<endl<<endl; }
Test.cpp
#include <iostream> #include <cstdlib> using namespace std; #include "PriorityQueue.h" int main(){ PriorityQueue<int,Compare<int> > queue; int init[10]={1,9,3,5,0,8,2,4,6,7}; for(int i=0;i<10;i++){ queue.Append(init[i]); } queue.Print(); queue.Delete(); queue.Print(); system("pause"); system("cls"); PriorityQueue<SpecialData,SpecialCmp> spe_queue; int init2[5][2]={{34,2},{64,1},{18,3},{24,2},{55,4}}; SpecialData data[5]; for(int i=0;i<5;i++){ data[i].m_npir=init2[i][1]; data[i].m_ntenor=init2[i][0]; } for(int i=0;i<5;i++){ spe_queue.Append(data[i]); } spe_queue.Print(); cout<<spe_queue.GetFront()<<endl<<endl; spe_queue.Delete(); spe_queue.Print(); return 0; }
==============================================================================
本博客已经废弃,不在维护。新博客地址:http://wenchao.ren
我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
实我是一个程序员
==============================================================================