优先级队列priority_queue源代码

本文介绍了《算法导论》第六章6.5节中的优先级队列概念,并提供了C++使用数组实现的优先级队列类Priority_queue的详细源代码。该类包括构造函数、push、pop、top、size和empty等方法,实现了最大堆的维护和操作。在测试用例中展示了如何插入元素、获取最大元素以及删除最大元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

数组实现.源代码:
#include<iostream>
using namespace std;

typedef 
int queue_entry;
#define maxque 20            // small number for test

class Priority_queue
{
public:
    Priority_queue()           
// constructor
        { count=0; }
    
void push(const queue_entry &item); 
    
void pop();                       // remove the largest key;
    const queue_entry& top();  // return the largest key

    
int  size ()
        
{    return count;    }         // return the number of the elements;
    bool empty() 
        
{    return count==0; }        // test if the queue is empty;

protected:
    
void max_heapify(int i);        // 维护最大堆性质
    queue_entry entry[maxque];     //  元素数组
    
int count;
}
;

const queue_entry&  Priority_queue::top()
{
    
if(count>0)
        
return entry[0];
    
else
        exit(
1);
}
   

void Priority_queue::max_heapify(int i) 
{
    
int largest, left, right;
    
bool flag;
    
do{
        flag
=0;
        left 
= 2 * i + 1
        right
= left + 1;
        
if(left < count && entry[left]>entry[i]) 
        
{
            largest
=left;
            flag
=1;
        }

        
else largest=i;
        
if(right < count && entry[right]>entry[largest])
        
{        
            largest
=right;
            flag
=1;
        }

        
if(flag) 
            swap(entry[i], entry[largest]);
        i 
= largest;
    }
    while(flag);
    
return;
}



void Priority_queue::push(const queue_entry &item)
{
    entry[count]
=item;
    
int i=count;
    count
++;
    
int parent=(i-1)/2;
    
while(i > 0 && entry[parent] < entry [i] )     
    
{
        swap (entry[i], entry[(i
-1)/2]);
        i 
= parent;
        parent 
= (i-1)/2;
    }

    
return;
}


void Priority_queue::pop()
{
    
if(count>0)
    
{
        entry[
0= entry[count-1];
        count
--;
        max_heapify(
0);
    }

    
else
        exit(
1);
    
return;
}


// test the Priority_queue
int main()
{
    Priority_queue q1;

   q1.push( 
10 );
   q1.push( 
35 );
   q1.push( 
35 );
   q1.push( 
30 );
   q1.push( 
25 ); 

   
int i;
   i 
= q1.size( );
   cout 
<< "The Priority_queue length is " << i << "." << endl;

   
const int& ii = q1.top( );
   cout 
<< "The element at the top of the Priority_queue is "
        
<< ii << "." << endl;

   q1.pop( );

   
int iii = q1.size( );
   cout 
<< "After a pop, the Priority_queue length is " 
        
<< iii << "." << endl;

   
const int& iv = q1.top( );
   cout 
<< "After a pop, the element at the top of the "
        
<< "priority_queue is " << iv << "." << endl;

   
return 0;
}
 

width="728" scrolling="no" height="90" frameborder="0" align="middle" src="http://download1.youkuaiyun.com/down3/20070601/01184120111.htm" marginheight="0" marginwidth="0">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值