织梦CMS - 轻松建站从此开始!

罗索

优先级队列priority_queue源代码

jackyhwei 发布于 2011-09-28 09:51 点击:次 
来自《算法导论》.第6章 优先级队列,函数名参照C++ STL中priority_queue
TAG:

<Introduction to algorithms> chapter 6.5
(《算法导论.第6章 优先级队列) ,,
函数名参照C++ STL中priority_queue

数组实现.源代码:

  1. #include<iostream> 
  2. using namespace std; 
  3.  
  4. typedef int queue_entry; 
  5. #define maxque 20  // small number for test 
  6.  
  7. class Priority_queue 
  8. public
  9.     Priority_queue()          // constructor 
  10.         { count=0; } 
  11.     void push(const queue_entry &item); 
  12.     void pop();               // remove the largest key; 
  13.     const queue_entry& top(); // return the largest key 
  14.  
  15.     int  size () 
  16.         {    return count;    }  // return the number of the elements; 
  17.     bool empty() 
  18.         {    return count==0; }  // test if the queue is empty; 
  19.  
  20. protected
  21.     void max_heapify(int i);     // 维护最大堆性质 
  22.     queue_entry entry[maxque];  //  元素数组 
  23.     int count; 
  24. }; 
  25.  
  26. const queue_entry&  Priority_queue::top() 
  27.     if(count>0) 
  28.         return entry[0]; 
  29.     else 
  30.         exit(1); 
  31. }   
  32.  
  33. void Priority_queue::max_heapify(int i) 
  34.     int largest, left, right; 
  35.     bool flag; 
  36.     do
  37.         flag=0; 
  38.         left = 2 * i + 1; 
  39.         right= left + 1; 
  40.         if(left < count && entry[left]>entry[i]) 
  41.         { 
  42.             largest=left; 
  43.             flag=1; 
  44.         } 
  45.         else largest=i; 
  46.         if(right < count && entry[right]>entry[largest]) 
  47.         {        
  48.             largest=right; 
  49.             flag=1; 
  50.         } 
  51.         if(flag) 
  52.             swap(entry[i], entry[largest]); 
  53.         i = largest; 
  54.     }    while(flag); 
  55.     return
  56.  
  57. void Priority_queue::push(const queue_entry &item) 
  58.     entry[count]=item; 
  59.     int i=count; 
  60.     count++; 
  61.     int parent=(i-1)/2; 
  62.     while(i > 0 && entry[parent] < entry [i] )     
  63.     { 
  64.         swap (entry[i], entry[(i-1)/2]); 
  65.         i = parent; 
  66.         parent = (i-1)/2; 
  67.     } 
  68.     return
  69.  
  70. void Priority_queue::pop() 
  71.     if(count>0) 
  72.     { 
  73.         entry[0] = entry[count-1]; 
  74.         count--; 
  75.         max_heapify(0); 
  76.     } 
  77.     else 
  78.         exit(1); 
  79.     return
  80.  
  81. // test the Priority_queue 
  82. int main() 
  83.     Priority_queue q1; 
  84.  
  85.    q1.push( 10 ); 
  86.    q1.push( 35 ); 
  87.    q1.push( 35 ); 
  88.    q1.push( 30 ); 
  89.    q1.push( 25 ); 
  90.  
  91.    int i; 
  92.    i = q1.size( ); 
  93.    cout << "The Priority_queue length is " << i << "." << endl; 
  94.  
  95.    const int& ii = q1.top( ); 
  96.    cout << "The element at the top of the Priority_queue is " 
  97.         << ii << "." << endl; 
  98.  
  99.    q1.pop( ); 
  100.  
  101.    int iii = q1.size( ); 
  102.    cout << "After a pop, the Priority_queue length is " 
  103.         << iii << "." << endl; 
  104.  
  105.    const int& iv = q1.top( ); 
  106.    cout << "After a pop, the element at the top of the " 
  107.         << "priority_queue is " << iv << "." << endl; 
  108.  
  109.    return 0; 
(秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201109/15062.html]
本文出处:《算法导论》 作者:秩名
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容