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

 更新时间:2017年08月28日 15:53:27   投稿:lqh  
这篇文章主要介绍了C++ 实现优先队列的简单实例的相关资料,希望通过本文能帮助大家实现优先队列,需要的朋友可以参考下

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

优先队列类模版实现:

BuildMaxHeap.h头文件:

#include<iostream> 
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(left<length&&a[left]>a[i]) 
  { 
    num=left; 
  } 
  //此处逻辑判断出错,开始为else if,发现不能正确排序,改为else之后正确 
  if(right<length&&a[right]>a[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]&&left<length) 
    { 
      largest=left; 
    } 
    //此处逻辑判断出错,开始为else if,发现不能正确排序,改为else之后正确 
    if(a[right]>a[largest]&&right<length) 
    { 
      largest=right; 
    } 
    if(num!=largest) 
    { 
      int temp; 
      temp=a[num]; 
      a[num]=a[largest]; 
      a[largest]=temp; 
      num=largest; 
      left=Left(num); 
      right=Right(num); 
    } 
    else  
    { 
      break; 
    } 
  } 
   
} 
void Build_Max_Heap(int a[],int length ) 
{ 
  int i; 
  for(i=(length-1)/2;i>=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<iostream> 
#include "BuileMaxHeap.h" 
using namespace std; 
#define MAX 100 
template <typename type>class PriorQueue 
{ 
private: 
  int length; 
  type a[MAX]; 
public: 
PriorQueue () 
{ 
  int i; 
  length=0; 
   
  for(i=0;i<MAX;i++) 
  { 
    a[i]=0; 
  } 
} 
  ~PriorQueue() 
  { 
     
  } 
  void BuildMaxHeapQueue(); 
  void Init(type b[],int len); 
  type Maxnum(); 
  bool DeleteMax(); 
  void IncreaseKey(int i,type key); 
  void Insert(type key); 
  void Print(); 
  void Sort(); 
}; 
template<typename type>void PriorQueue<type>::Init(type b[],int len) 
{ 
  int i; 
  this->length=len; 
  if(len<=0) 
  { 
    cout<<"Init failed !"<<endl; 
  } 
  for(i=0;i<len;i++) 
  { 
    a[i]=b[i]; 
  } 
  BuildMaxHeapQueue(); 
} 
template<typename type>void PriorQueue<type>::BuildMaxHeapQueue() 
{ 
  Build_Max_Heap(a,length); 
} 
template<typename type>type PriorQueue<type>::Maxnum() 
{ 
  if(length<=0) 
  { 
    cout<<"the queue is empty!"<<endl; 
    return -1; 
  } 
  return a[0]; 
} 
template<typename type>bool PriorQueue<type>::DeleteMax() 
{ 
  if(length<=0) 
  { 
    cout<<"the queue is empty!"<<endl; 
    return 0; 
  } 
  a[0]=a[length-1]; 
  length-=1; 
  Max_Heapify1(a,length,0); 
  return 1; 
} 
template<typename type>void PriorQueue<type>::IncreaseKey(int i,type key) 
{ 
  int num=i; 
  if(key<a[num]) 
  { 
    cout<<"key is error!"<<endl; 
    return ; 
  } 
  a[num]=key; 
  while(num>0&&a[Parent(num)]<a[num]) 
  { 
    type temp; 
    temp=a[num]; 
    a[num]=a[Parent(num)]; 
    a[Parent(num)]=temp; 
    num=Parent(num); 
  } 
} 
 
template<typename type>void PriorQueue<type>::Insert(type key) 
{ 
  if(length>=MAX) 
  { 
    cout<<"the queue is full can't add more!"<<endl; 
    return ; 
  } 
  length+=1; 
  a[length-1]=key; 
  IncreaseKey(length-1,key); 
} 
template<typename type>void PriorQueue<type>::Print() 
{ 
  int i; 
  for(i=0;i<length;i++) 
  { 
    cout<<a[i]<<" "; 
  } 
  cout<<endl; 
} 
template<typename type>void PriorQueue<type>::Sort() 
{ 
  Heap_Sort(a,length); 
} 
 

main.cpp

#include"PriorQueue.h" 
#include<iostream> 
using namespace std; 
 
int main() 
{ 
  PriorQueue<int> node; 
  int b[10]={4,1,3,2,16,9,10,14,8,7}; 
  node.Init(b,10); 
  int i; 
  node.Print(); 
  cout<<endl; 
  cout<<node.Maxnum()<<endl; 
  node.DeleteMax(); 
  node.Print(); 
  node.Insert(232); 
  node.Sort(); 
  node.Print(); 
  return 0; 
} 

以上就是数据结构优先队列的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)

    C++11新特性之智能指针(shared_ptr/unique_ptr/weak_ptr)

    这篇文章主要介绍了C++11新特性之智能指针,包括shared_ptr, unique_ptr和weak_ptr的基本使用,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • C++ OpenCV实现抖音"蓝线挑战"特效

    C++ OpenCV实现抖音"蓝线挑战"特效

    这篇文章主要介绍了如何使用OpenCV C++ 实现抖音上的特效“蓝线挑战”。文中的示例代码讲解详细,对我们学习OpenCV有一定的帮助,需要的可以参考一下
    2022-01-01
  • 用C语言实现计算器功能

    用C语言实现计算器功能

    这篇文章主要为大家详细介绍了用C语言实现计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • c++ sqlite3如何利用事务(BEGIN;COMMIT;)批量操作

    c++ sqlite3如何利用事务(BEGIN;COMMIT;)批量操作

    这篇文章主要介绍了c++ sqlite3如何利用事务(BEGIN;COMMIT;)批量操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • C++实现酒店管理系统

    C++实现酒店管理系统

    这篇文章主要为大家详细介绍了C++实现酒店管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 关于读取popen输出结果时未截断字符串导致的命令行注入详解

    关于读取popen输出结果时未截断字符串导致的命令行注入详解

    这篇文章主要给大家介绍了关于读取popen输出结果时未截断字符串导致的命令行注入的相关资料,文中通过图文及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-03-03
  • 解析c++中的默认operator=操作的详解

    解析c++中的默认operator=操作的详解

    本篇文章是对c++中的默认operator=操作的应用进行了详细的分析介绍。需要的朋友参考下
    2013-05-05
  • C++ 基于BFS算法的走迷宫自动寻路的实现

    C++ 基于BFS算法的走迷宫自动寻路的实现

    这篇文章主要为大家介绍了C++ 基于BFS算法实现走迷宫自动寻路,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C/C++实现遍历文件夹最全方法总结

    C/C++实现遍历文件夹最全方法总结

    这篇文章主要为大家介绍了C/C++实现遍历文件夹功能的最全方法总结,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-09-09
  • C++实现学生住宿管理系统

    C++实现学生住宿管理系统

    这篇文章主要为大家详细介绍了C++实现学生住宿管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论