网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C++怎么实现优先队列-创新互联

这篇文章主要介绍C++怎么实现优先队列,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

为阳春等地区用户提供了全套网页设计制作服务,及阳春网站建设行业解决方案。主营业务为成都做网站、网站设计、阳春网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

C++ 实现优先队列的简单实例

优先队列类模版实现:

BuildMaxHeap.h头文件:

#include 
using namespace std; 
#define Left(i) i*2+1 
#define Right(i) i*2+2 
#define Parent(i) (i-1)/2 
void Max_Heapify(int a[],int length,int i) 
{ 
  int left,right; 
  left=Left(i); 
  right=Right(i); 
  int num=i; 
  if(lefta[i]) 
  { 
    num=left; 
  } 
  //此处逻辑判断出错,开始为else if,发现不能正确排序,改为else之后正确 
  if(righta[num]) 
  { 
    num=right; 
  } 
  if(num!=i) 
  { 
    int temp; 
    temp=a[i]; 
    a[i]=a[num]; 
    a[num]=temp; 
    Max_Heapify(a,length,num); 
  } 
} 
//此处添加利用循环方式代替递归Max_Heapify的函数,该函数可以替代Max_Heapify 
//在某些情况下能取得更好 的效果 
void Max_Heapify1(int a[],int length,int i) 
{ 
  int left,right,num=i,largest=i; 
  left=Left(i); 
  right=Right(i); 
  while(1) 
  { 
    if(a[left]>a[num]&&lefta[largest]&&right=0;i--) 
  { 
    Max_Heapify1(a,length,i); 
  } 
} 
 
void Heap_Sort(int a[],int length) 
{ 
  Build_Max_Heap(a,length); 
  int i; 
  int temp; 
  for(i=length-1;i>=1;i--) 
  { 
    temp=a[i]; 
    a[i]=a[0]; 
    a[0]=temp; 
    length-=1; 
    Max_Heapify1(a,length,0); 
  } 
}

PriorQUeue.h

#include 
#include "BuileMaxHeap.h" 
using namespace std; 
#define MAX 100 
template class PriorQueue 
{ 
private: 
  int length; 
  type a[MAX]; 
public: 
PriorQueue () 
{ 
  int i; 
  length=0; 
   
  for(i=0;ivoid PriorQueue::Init(type b[],int len) 
{ 
  int i; 
  this->length=len; 
  if(len<=0) 
  { 
    cout<<"Init failed !"<void PriorQueue::BuildMaxHeapQueue() 
{ 
  Build_Max_Heap(a,length); 
} 
templatetype PriorQueue::Maxnum() 
{ 
  if(length<=0) 
  { 
    cout<<"the queue is empty!"<bool PriorQueue::DeleteMax() 
{ 
  if(length<=0) 
  { 
    cout<<"the queue is empty!"<void PriorQueue::IncreaseKey(int i,type key) 
{ 
  int num=i; 
  if(key0&&a[Parent(num)]void PriorQueue::Insert(type key) 
{ 
  if(length>=MAX) 
  { 
    cout<<"the queue is full can't add more!"<void PriorQueue::Print() 
{ 
  int i; 
  for(i=0;ivoid PriorQueue::Sort() 
{ 
  Heap_Sort(a,length); 
}

main.cpp

#include"PriorQueue.h" 
#include 
using namespace std; 
 
int main() 
{ 
  PriorQueue node; 
  int b[10]={4,1,3,2,16,9,10,14,8,7}; 
  node.Init(b,10); 
  int i; 
  node.Print(); 
  cout<

以上是“C++怎么实现优先队列”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联网站建设公司行业资讯频道!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:C++怎么实现优先队列-创新互联
转载注明:http://bjjierui.cn/article/pgssg.html

其他资讯