基于堆的优先队列的实现
来源:互联网 发布:荣威erx5网络连接错误 编辑:程序博客网 时间:2024/03/28 21:40
优先队列的异常处理类
#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
优先队列类的头文件
#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
优先队列的实现文件
#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 !");}}
测试优先队列
#include<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<<"the priority queue is :"<<endl;while(!apq.empty()){apq.pqDelete(item);cout<<item.getKey()<<endl;}}catch(PQueueException &e){cout<<e.what()<<endl;}return 0;}
- 基于堆的优先队列的实现
- 基于堆的优先队列实现
- 基于堆的优先队列
- 基于堆的优先队列
- 基于堆的优先队列
- 基于堆的优先队列
- 基于二叉堆实现的优先队列和堆排序
- 用java实现一个基于堆排序的优先队列
- 堆的实现、堆排序、优先队列
- 《算法》基于堆的优先队列
- 数据结构与算法-堆、基于堆实现的优先队列、堆排序
- 基于堆的优先队列和堆排序
- 基于堆的优先队列及堆排序
- 最大堆及基于最大堆的最大优先队列
- 最小堆及基于最小堆的最小优先队列
- 堆和优先队列的实现
- 从堆到优先队列的实现
- 优先队列的实现--二叉堆
- MAC 10.6.8升级到LION
- 遗传算法之花朵进化
- 使用LINQ分页时需要注意的一个细节
- Ubuntu的apt-get代理设置
- Android流量统计TrafficStats类的使用
- 基于堆的优先队列的实现
- 2_FFT算法的实现
- android 手势 API的使用
- Spring 的MVC框架
- isdigit <ctype.h> <cctype>
- Android 4.0的12大新特性
- 08-Queue
- Linux--常用命令--uname
- 09-BadLock