优先队列(priorityqueue)
队列是先进先出的线性表,顾名思义,优先队列则是元素有优先级的队列,出列的顺序由元素的优先级决定。从优先队列中删除元素是根据优先权的高低次序,而不是元素进入队列的次序。优先队列的典型应用是机器调度等。
假设我们对机器服务进行收费。每个用户每次使用机器所付费用都是相同的,但每个用户所需要服务时间都不同。为获得最大利润,假设只要有用户机器就不会空闲,我们可以把等待使用该机器的用户组织成一个最小优先队列,优先权即为用户所需服务时间。当一个新的用户需要使用机器时,将他 /她的请求加入优先队列。一旦机器可用,则为需要最少服务时间(即具有最高优先权)的用户提供服务。如果每个用户所需时间相同,但用户愿意支付的费用不同,则可以用支付费用作为优先权,一旦机器可用,所交费用最多的用户可最先得到服务,这时就要选择最大优先队列。
1.概念
优先队列( priority queue)是0个或多个元素的集合,每个元素都有一个优先权或值,对优先队列执行的操作有 1) 查找; 2) 插入一个新元素; 3) 删除。在最小优先队列( min priority q u e u e)中,查找操作用来搜索优先权最小的元素,删除操作用来删除该元素;对于最大优先队列( max priority queue),查找操作用来搜索优先权最大的元素,删除操作用来删除该元素。优先权队列中的元素可以有相同的优先权,查找与删除操作可根据任意优先权进行。
描述优先队列最简单的方法是采用无序线性表。假设有一个具有n个元素的优先队列,如果采用公式化的线性表描述,那么插入操作可以十分方便的在表的右端末尾执行,插入操作的时间为O(1)。删除时必须查找优先权最大的元素,因此所需时间为O(n)。
若采用有序的线性表,则插入为O(n),删除为O(1)
2.实现
由上面描述可以知道,我们可以在线性表的基础上完成优先队列,只需要改变删除的操作,当删除时找到最大或最小的值即可。
我们可以直接继承线性表类线性表的2种实现方式:数组和链表
线性表的公式化实现:
#ifndef LINEARLIST_H
#define LINEARLIST_H
#include<iostream>
#include<cstdlib>
#include<new>
using std::cout;
using std::endl;
template<class T>
class LinearList
{
public:
LinearList(int MaxListSize=);//构造函数
virtual ~LinearList();
bool IsEmpty()const
{
return length==;
}
int Length()const {return length;}
bool Find(int k,T& x)const;//返回第K个元素到中
int Search(T& x)const;//返回x的位置
LinearList<T>& Delete(int k,T& x);//删除位置k的元素,并将元素值存到x
LinearList<T>& Insert(int k,const T& x);//将x插入到k位置之后
void Output(std::ostream& out)const;//输出到流 protected:
int length;//线性表当前长度
int MaxSize;//最大长度
T *element;//线性表数组
}; class NoMem
{
public :
NoMem(){
cout<<"No Memory"<<endl;
//std::exit(1);
} }; class OutofBounds
{
public :
OutofBounds()
{
cout<<"Out of Bounds"<<endl;
//std::exit(1);
}
}; void my_new_handler()
{
throw NoMem();
} template<class T>
LinearList<T>::LinearList(int MaxListSize)
{
std::new_handler old_Handler=std::set_new_handler(my_new_handler);
MaxSize=MaxListSize;
element=new T[MaxSize];
length=; } template<class T>
LinearList<T>::~LinearList()
{
delete[]element;
MaxSize=;
length=;
} template<class T>
bool LinearList<T>::Find(int k,T&x)const
{
if(k<||k>length)
return false;
x=element[k-];
return true;
} template<class T>
int LinearList<T>::Search(T &x)const
{
int i=;
while(i<length&&element[i]!=x)
{
i++;
}
if(i==length) return ;
else return i+;
} template<class T>
LinearList<T>& LinearList<T>::Delete(int k,T &x)
{
if(Find(k,x))//存在位置k
{
for(int i=k;i<length;++i)
{
element[i-]=element[i];//k之后元素向前移动一位
}
length--;
return *this;
}
else
{
throw OutofBounds();
}
} template<class T>
LinearList<T>& LinearList<T>::Insert(int k,const T &x)
{
if(k<||k>length)
{
throw OutofBounds();
}
else if(length==MaxSize)
{
throw NoMem();
}
else
{
for(int i=length;i>k;--i)
{
element[i]=element[i-];//k之后元素向后移动一位
}
element[k]=x;
length++;
return *this;
}
} template<class T>
void LinearList<T>::Output(std::ostream& out)const
{
for(int i=;i<length;i++)
{ out<<element[i]<<" ";
}
} template<class T>
std::ostream& operator<<(std::ostream &out,const LinearList<T>& x)
{
x.Output(out);
return out;
} #endif // LINEARLIST_H
最大优先队列:
#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H
#include "LinearList.h" template<typename T>
class PriorityQueue:public LinearList<T>
{
public:
PriorityQueue(int MaxListSize=):LinearList<T>::LinearList(MaxListSize){};
PriorityQueue<T>& Insert(const T& x);
PriorityQueue<T>& Delete(T& x);
T Max() const;
~PriorityQueue(){};
//void Output(std::ostream& out)const;//输出到流
friend ostream& operator<< <>(ostream& output, const PriorityQueue<T>& x);
private:
size_t MaxIndex() const; }; //末端插入
template<typename T>
PriorityQueue<T>& PriorityQueue<T>::Insert(const T& x)
{
if (length>=MaxSize)
{
throw NoMem();
} element[length++] = x;
return *this;
} //找到最大值的索引(下标)
template<typename T>
size_t PriorityQueue<T>::MaxIndex() const
{
if (length == )
{
throw OutofBounds();
}
size_t maxNum = ;
for (size_t i = ; i < length; ++i)
{
if (element[i]>element[maxNum])
{
maxNum = i;
}
} return maxNum;
} //返回最大值
template<typename T>
T PriorityQueue<T>::Max() const
{
if (length==)
{
throw OutofBounds();
}
size_t maxNum = MaxIndex(); return element[maxNum];
} //取出最大值
template<typename T>
PriorityQueue<T>& PriorityQueue<T>::Delete(T& x)
{
if (length==)
{
throw OutofBounds();
} size_t maxindex = MaxIndex();
x = element[maxindex]; //元素前移
for (size_t i = maxindex; i < length-;++i)
{
element[i] = element[i + ];
}
--length;
return *this;
}
/*
template<typename T>
void PriorityQueue<T>::Output(std::ostream& out) const
{
if (length==0)
{
throw OutofBounds();
}
for (size_t i = 0; i < length;++i)
{
out << element[i]<<' ';
} out << endl;
}
*/
template<typename T>
ostream& operator<<(ostream& output,const PriorityQueue<T>& x)
{
x.Output(output);
return output;
}
#endif
测试:
#include<iostream>
using namespace std; #include "PriorityQueue.h" int main()
{
PriorityQueue<int> testQ;
testQ.Insert();
testQ.Insert();
testQ.Insert();
testQ.Insert(); cout << "Queue is: " << endl;
cout << testQ << endl;
cout << "Queue size is: " << testQ.Length();
cout << endl;
cout << "Max in Queue is: " << testQ.Max();
cout << endl; int x;
testQ.Delete(x);
cout << "element deleted is: " << x<<endl; cout << "Queue is: " << endl;
cout << testQ << endl;
cout << "Queue size is: " << testQ.Length();
cout << endl;
cout << "Max in Queue is: " << testQ.Max();
cout << endl; return ;
}
优先队列(priorityqueue)的更多相关文章
- 【Java源码】集合类-优先队列PriorityQueue
一.类继承关系 public class PriorityQueue<E> extends AbstractQueue<E> implements java.io.Serial ...
- [Swift]实现优先队列PriorityQueue
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Java的优先队列PriorityQueue详解
一.优先队列概述 优先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序, 可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类 对于基本数据类型的包装器 ...
- Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节
目录 Java优先队列PriorityQueue的各种打开方式以及一些你不知道的细节 优先队列的默认用法-从小到大排序 对String类用优先队列从大到小排序 通过自定义比较器对自定义的类进行从小到大 ...
- .NET 6 优先队列 PriorityQueue 实现分析
在最近发布的 .NET 6 中,包含了一个新的数据结构,优先队列 PriorityQueue, 实际上这个数据结构在隔壁 Java中已经存在了很多年了, 那优先队列是怎么实现的呢? 让我们来一探究竟吧 ...
- [Swift]优先队列PriorityQueue(自定义数据结构)
优先队列[priority queue] 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除. 优先队列特点:在优先队列中,元素被赋予优先级. 当访问元素时,具有最高优先级的元素最先 ...
- 优先队列PriorityQueue实现 大小根堆 解决top k 问题
转载:https://www.cnblogs.com/lifegoesonitself/p/3391741.html PriorityQueue是从JDK1.5开始提供的新的数据结构接口,它是一种基于 ...
- Python 标准库 —— 队列(Queue,优先队列 PriorityQueue)
优先队列,有别于普通队列的先入先出(虽然字面上还是队列,但其实无论从含义还是实现上,和普通队列都有很大的区别),也有别于栈的先入后出.在实现上,它一般通过堆这一数据结构,而堆其实是一种完全二叉树,它会 ...
- 优先队列PriorityQueue&Lambda&Comparator
今天翻阅<Labuladuo的算法小抄>时发现在使用优先队列的PriorityQueue解决一道hard题时(leetCode 23),出现了如下代码: ListNode mergeKLi ...
随机推荐
- Oracle使用NLSSORT函数实现汉字的排序
1).按拼音首字母排序 SELECT * FROM T_TABLE ORDER BY NLSSORT(NAME, 'NLS_SORT=SCHINESE_PINYIN_M'); 2).按笔画排序SELE ...
- ubuntu下ffmpeg的安装,实现支持3gpp等转换
最近上线的项目,语音格式转码需要调试3gpp,所以需要再spx,3gpp,3gp等格式之间转换,特记录基于ubuntu环境下的环境ffmpeg部署细则 安装测试环境:ubuntu 14.04 64bi ...
- android XML解析之DOM解析方式
DOM 解析方式步骤: 第一步:首选需要获得DOM解析器工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ...
- React-Native 获取node.js提供的接口
一个简单的React-Native 获取node.js提供的接口的实现 一.node.js var http = require("http"); var url = requir ...
- session的存储方式和配置
Session又称为会话状态,是Web系统中最常用的状态,用于维护和当前浏览器实例相关的一些信息.我们控制用户去权限中经常用到Session来存储用户状态,这篇文章会讲下Session的存储方式.在w ...
- mvc 客户端验证
@model MvcApplication1.Models.ViewClass @{ ViewBag.Title = "View2"; } @******引用这两个js实现客户端的 ...
- hdu2952Counting Sheep
Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, ...
- java 成神之路
一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...
- Eclipse编译Arduino程序不能使用串口函数Serial.begin解决办法
在Arduino官方的编译器当中Serial.begin(9600);初始化语句是可以直接使用的,而到Eclipse当中,同样的语句却不能用了.会出现下面的问题: 显然,这是Eclipse没有找到Se ...
- 认识Java里面的Thread
在一个特定的主线程执行的过程中,如果我们还需要在主线程的过程中插播一个线程,做其他动作.那么我们就可以利用Java的Thread类,创建一个新的线程. 一:线程简单实现的三种方式 (1)第一种创建线程 ...