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

作为最小堆应用,实现了另一个实用的类----优先队列.优先队列有着广泛的应用,在操作系统中,许多消息队列、等待队列等,使用了优先队列,在算法中,我们常用优先队列来实现广度搜索、贪心算法等。
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;
}

你可能感兴趣的:(C++,c,算法,C#,Blog)