fuliangliang的Blog

合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。

常用类之三----最小堆实现优先队列

Priority_Queue.h
#include"MinHeap.h"
template<class T>
class Priority_Queue{
 public:
  Priority_Queue();
  ~Priority_Queue();
  void Push(const T &x);
     T Pop();
  bool IsEmpty();
  bool IsFull();
 private:
  MinHeap<T> *m_heap;
  const int maxSize;
};

template<class T>
Priority_Queue<T>::Priority_Queue():maxSize(100){
     m_heap = new MinHeap<T>(maxSize);
}

template<class T>
Priority_Queue<T>::~Priority_Queue(){
  delete m_heap;
}

template<class T>
void Priority_Queue<T>::Push(const T &x){
  m_heap->Insert(x);
}

template<class T>
T  Priority_Queue<T>::Pop(){
  T temp;
  m_heap->RemoveMin(temp);
  return temp;
}

template<class T>
bool Priority_Queue<T>::IsEmpty(){
  return m_heap->IsEmpty();
}

template<class T>
bool Priority_Queue<T>::IsFull(){
  return m_heap->IsFull();
}

mainApp.cpp测试文件:

#include<iostream>
#include"Priority_Queue.h"
using namespace std;

void main(){
 int a[5] = {3,2,1,4,5};
 int i;
 Priority_Queue<int> p_queue;
 for(i = 0; i < 5; i++){
  p_queue.Push(a[i]);
 }
 while(!p_queue.IsEmpty()){
  cout<<p_queue.Pop()<<" ";
 }
 cout<<endl;
}

阅读更多
文章标签: delete class 测试
个人分类: Algorithm
上一篇实用类之二-----最大堆的实现
下一篇最小堆应用---用最小堆实现huffman树
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭