优先队列的异常处理类

view plaincopy to clipboardprint?

  1. #ifndef PQUEUEEXCEPTION_H_
  2. #define PQUEUEEXCEPTION_H_
  3. #include<stdexcept>
  4. #include<string>
  5. class PQueueException :public std::logic_error 
  6. public: 
  7.         PQueueException(const std::string &message="") 
  8.             :std::logic_error(message.c_str()) 
  9.         {} 
  10. }; 
  11. #endif

#ifndef PQUEUEEXCEPTION_H_ #define PQUEUEEXCEPTION_H_ #include<stdexcept> #include<string> class PQueueException :public std::logic_error { public: PQueueException(const std::string &message="") :std::logic_error(message.c_str()) {} }; #endif

优先队列类的头文件

view plaincopy to clipboardprint?

  1. #ifndef PRIORITYQUEUE_H_
  2. #define PRIORITYQUEUE_H_
  3. #include"heap_array.h"
  4. #include"PQueueexception.h"
  5. typedef HeapItemType PQueueItemType; 
  6. class PriorityQueue 
  7. public: 
  8.         PriorityQueue(); 
  9. //default constructor ,copy constructor and 
  10. //destrutor are supplied by the complier
  11.  
  12. //priority-queue operations:
  13. virtual bool pqempty()const; 
  14. virtual void pqInsert(const PQueueItemType &newitem) 
  15. throw(PQueueException); 
  16. virtual void pqDelete(PQueueItemType &rootitem) 
  17. throw(PQueueException); 
  18. private: 
  19.         Heap h; 
  20. }; 
  21. #endif

#ifndef PRIORITYQUEUE_H_ #define PRIORITYQUEUE_H_ #include"heap_array.h" #include"PQueueexception.h" typedef HeapItemType PQueueItemType; class PriorityQueue { public: PriorityQueue(); //default constructor ,copy constructor and //destrutor are supplied by the complier //priority-queue operations: virtual bool pqempty()const; virtual void pqInsert(const PQueueItemType &newitem) throw(PQueueException); virtual void pqDelete(PQueueItemType &rootitem) throw(PQueueException); private: Heap h; }; #endif

优先队列的实现文件

view plaincopy to clipboardprint?

  1. #include"priorityQueue.h"
  2. PriorityQueue::PriorityQueue() 
  3.     :h() 
  4. {} 
  5. bool PriorityQueue::pqempty()const
  6. return h.empty(); 
  7. void PriorityQueue::pqInsert(const PQueueItemType &newitem) 
  8. throw(PQueueException) 
  9. try{ 
  10.         h.heapInsert(newitem); 
  11.     } 
  12. catch(HeapException &e) 
  13.     { 
  14. throw PQueueException("PQueueException : insert item failed !"); 
  15.     } 
  16. void PriorityQueue::pqDelete(PQueueItemType &rootitem) 
  17. throw(PQueueException) 
  18. try{ 
  19.         h.heapDelete(rootitem); 
  20.     }catch(PQueueException &e) 
  21.     { 
  22. throw PQueueException("PQueueException :delete item failed !"); 
  23.     } 

#include"priorityQueue.h" PriorityQueue::PriorityQueue() :h() {} bool PriorityQueue::pqempty()const { return h.empty(); } void PriorityQueue::pqInsert(const PQueueItemType &newitem) throw(PQueueException) { try{ h.heapInsert(newitem); } catch(HeapException &e) { throw PQueueException("PQueueException : insert item failed !"); } } void PriorityQueue::pqDelete(PQueueItemType &rootitem) throw(PQueueException) { try{ h.heapDelete(rootitem); }catch(PQueueException &e) { throw PQueueException("PQueueException :delete item failed !"); } }

测试优先队列

view plaincopy to clipboardprint?

  1. #include<iostream>
  2. #include"priorityQueue.h"
  3. using namespace std; 
  4. int main() 
  5.     PriorityQueue apq; 
  6.  
  7. try{ 
  8.     apq.pqInsert(KeyItem("sort")); 
  9.     apq.pqInsert(KeyItem("exception")); 
  10.     apq.pqInsert(KeyItem("STL")); 
  11.     apq.pqInsert(KeyItem("queue")); 
  12.     apq.pqInsert(KeyItem("priority")); 
  13.     apq.pqInsert(KeyItem("heap")); 
  14.     apq.pqInsert(KeyItem("item")); 
  15.     apq.pqInsert(KeyItem("terrda")); 
  16.     apq.pqInsert(KeyItem("absort")); 
  17.     apq.heapInsert(KeyItem("oppo")); 
  18. //  aheap.display();
  19.     KeyItem item; 
  20.     cout<&lt;"the priority queue is :"&lt;&lt;endl; 
  21. while(!apq.empty()) 
  22.     { 
  23.         apq.pqDelete(item); 
  24.         cout&lt;&lt;item.getKey()&lt;&lt;endl; 
  25.     } 
  26.     }catch(PQueueException &e) 
  27.     { 
  28.         cout&lt;&lt;e.what()&lt;&lt;endl; 
  29.     } 
  30. return 0; 

#include&lt;iostream> #include"priorityQueue.h" using namespace std; int main() { PriorityQueue apq; try{ apq.pqInsert(KeyItem("sort")); apq.pqInsert(KeyItem("exception")); apq.pqInsert(KeyItem("STL")); apq.pqInsert(KeyItem("queue")); apq.pqInsert(KeyItem("priority")); apq.pqInsert(KeyItem("heap")); apq.pqInsert(KeyItem("item")); apq.pqInsert(KeyItem("terrda")); apq.pqInsert(KeyItem("absort")); apq.heapInsert(KeyItem("oppo")); // aheap.display(); KeyItem item; cout&lt;&lt;"the priority queue is :"&lt;&lt;endl; while(!apq.empty()) { apq.pqDelete(item); cout&lt;&lt;item.getKey()&lt;&lt;endl; } }catch(PQueueException &e) { cout&lt;&lt;e.what()&lt;&lt;endl; } return 0; }