普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。通常采用堆数据结构来实现。
先来看一下堆的概念,堆是二叉堆的简称,是一种每个节点的元素都大于两个子节点的完全二叉树,在优先队列中一般用数组表示。
#include<iostream>
#include<vector>
using namespace std;
class priotyquene
{
private:
vector<int> list;
public:
void swap_(int m, int n)
{
int tmp = list[m];
list[m] = list[n];
list[n] = tmp;
}
priotyquene()
{
list.push_back(0);
}
bool isempty()
{
return list.size()-2 == 0;
}
void insert(int key)
{
list.push_back(key);
swim(list.size()-2);
}
void swim(int point)
{
while (point > 1 && compare(point / 2, point))
{
swap_(point/2, point);
point /= 2;
}
}
int compare(int m,int n)
{
return list[m] < list[n];
}
int demax()
{
vector<int>::iterator iter = list.end();
int max = list[1];
list[1] = list[list.size() - 1];
list.erase(iter);
sink(1);
}
void sink(int point)
{
while (2*point <= list.size() - 2)
{
int j = 2 * point;
if (j < list.size() - 2 && compare(j, j + 1))
j++;
if (!compare(point, j))
break;
swap_(point, j);
point = j;
}
}
};