欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

C++怎么實(shí)現(xiàn)優(yōu)先隊(duì)列-創(chuàng)新互聯(lián)

這篇文章主要介紹C++怎么實(shí)現(xiàn)優(yōu)先隊(duì)列,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

為陽春等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及陽春網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、陽春網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

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; 
}

以上是“C++怎么實(shí)現(xiàn)優(yōu)先隊(duì)列”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站aaarwkj.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

本文題目:C++怎么實(shí)現(xiàn)優(yōu)先隊(duì)列-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://aaarwkj.com/article36/pgssg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站策劃網(wǎng)站內(nèi)鏈、定制網(wǎng)站網(wǎng)站制作、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
亚洲一区二区三区免费观看视频| 国产精品日韩一区视频| 亚洲av成人精品日韩一区麻豆 | 国产成人拍国产亚洲精品| 亚洲精品你懂的av在线| 国产精品九九久久精品三级| 亚洲欧美综合伊人看片综合| 蜜臀91精品视频在线观看| 四虎国产精品久久久久久网址| 日韩中文字幕资源一区| av毛片天堂在线观看| 成人一区二区三区乱码| 熟女人妻精品一二三四| 亚洲av香蕉综合一区| 下一篇亚洲一区二区三区| 97乱碰视频在线观看| av一区二区三区不卡在线看 | 久久亚洲综合精品少妇| 国产一区二区三区不卡视频| 欧美成人精品三级在线网站| 日本韩国国语对白一区二区三区 | 欧美国产日韩二区一区在线| 91天美精东果冻麻豆| 中文字幕制服国产精品| 97在线公开免费视频| 成人av男人天堂东京热| 亚洲欧美韩国日本成人综合| 日韩专区欧美二区国产| 日本欧美高清一区二区| 91九色中文视频在线观看| 欧美一区二区男人天堂| 精品偷拍污视频一区二区| 丝袜美腿精尽福利视频网址大全| 成人嚼牙特别黑黄怎么办| 91久久亚洲综合精品日本| 欧美亚洲尤物久久精品| 日本国产在线一区二区| 亚洲欧美日韩国产在线一区| 国产伦理在线观看一区二区| 亚洲码av一区二区三区| 国产精品亚洲视频欧美视频|