C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例
C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例
優(yōu)先隊(duì)列類模版實(shí)現(xiàn):
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;
}
//此處邏輯判斷出錯(cuò),開始為else if,發(fā)現(xiàn)不能正確排序,改為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);
}
}
//此處添加利用循環(huán)方式代替遞歸Max_Heapify的函數(shù),該函數(shù)可以替代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;
}
//此處邏輯判斷出錯(cuò),開始為else if,發(fā)現(xiàn)不能正確排序,改為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;
}
以上就是數(shù)據(jù)結(jié)構(gòu)優(yōu)先隊(duì)列的實(shí)例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- c++優(yōu)先隊(duì)列(priority_queue)用法詳解
- c++優(yōu)先隊(duì)列用法知識(shí)點(diǎn)總結(jié)
- C++優(yōu)先隊(duì)列用法案例詳解
- 詳解c++優(yōu)先隊(duì)列priority_queue的用法
- C++高級(jí)數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列
- C++實(shí)現(xiàn)優(yōu)先隊(duì)列的示例詳解
- C++示例詳解Prim算法與優(yōu)先隊(duì)列
- 深入了解C++優(yōu)先隊(duì)列(priority_queue)的使用方法
- C++中STL的優(yōu)先隊(duì)列priority_queue詳解
- C++優(yōu)先隊(duì)列的使用小結(jié)
- C++的實(shí)現(xiàn)優(yōu)先隊(duì)列(Priority?Queue)的實(shí)現(xiàn)
相關(guān)文章
C++11新特性之智能指針(shared_ptr/unique_ptr/weak_ptr)
這篇文章主要介紹了C++11新特性之智能指針,包括shared_ptr, unique_ptr和weak_ptr的基本使用,感興趣的小伙伴們可以參考一下2016-08-08
C++ OpenCV實(shí)現(xiàn)抖音"藍(lán)線挑戰(zhàn)"特效
這篇文章主要介紹了如何使用OpenCV C++ 實(shí)現(xiàn)抖音上的特效“藍(lán)線挑戰(zhàn)”。文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)OpenCV有一定的幫助,需要的可以參考一下2022-01-01
c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作
這篇文章主要介紹了c++ sqlite3如何利用事務(wù)(BEGIN;COMMIT;)批量操作,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
關(guān)于讀取popen輸出結(jié)果時(shí)未截?cái)嘧址畬?dǎo)致的命令行注入詳解
這篇文章主要給大家介紹了關(guān)于讀取popen輸出結(jié)果時(shí)未截?cái)嘧址畬?dǎo)致的命令行注入的相關(guān)資料,文中通過圖文及示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
C++ 基于BFS算法的走迷宮自動(dòng)尋路的實(shí)現(xiàn)
這篇文章主要為大家介紹了C++ 基于BFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
C/C++實(shí)現(xiàn)遍歷文件夾最全方法總結(jié)
這篇文章主要為大家介紹了C/C++實(shí)現(xiàn)遍歷文件夾功能的最全方法總結(jié),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09
C++實(shí)現(xiàn)學(xué)生住宿管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)學(xué)生住宿管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

