C语言优先队列作用,C语言实现优先队列(priority queue)

堆排序是一个比较优秀的算法,堆这种数据结构在现实生活中有很多的应用,比如堆可以作为一个优先队列来使用,作为一个高效的优先队列,它与堆的结构一样,都有最大优先队列,最小优先队列.优先队列priority queue 是一种用来维护一组元素构成的集合S的数据结构,每一个元素都有一个相关的值,称为关键字(key)。

最大优先队列包含以下操作:

math?formula=INSERT(%20S%2Cx)%20%3A将元素x插入到S的集合中,等价于

math?formula=S%3DS%5Ccup%20%5Cleft%5C%7B%20x%20%5Cright%5C%7D%20

math?formula=MAXIMUM%20(S)%3A%0A返回S中最大元素;

math?formula=EXTRACT-MAX(%20S)%3A%20返回并且删除S中最大元素;

math?formula=INCREASE-KEY(S%2C%20x%2Ckey)%20%3A将元素x的关键字增加到key,要求

math?formula=key%5Cgeq%20x%20

同样的,最小优先队列操作也包括:

math?formula=INSERT(S%2C%20x)%20,

math?formula=MINIMUM(S%20)%20,

math?formula=EXTRACT-MIN(S%20)%20,

math?formula=DECREASE(S%2C%20x%2Ckey)%20。只不过是对最小值进行操作。

在这里主要讨论最大优先队列,其应用很多,在共享计算机作业系统就是,类似于早期的unix主机,管理员root可以设置n个不同的用户,以及各个用户不同的操作权限,从主机那里接出多个终端,每个操作人员(程序员)在自己的工作终端 ,感觉像是自己拥有自己独立的作业主机一样,其实不是,通过一些任务调度来实现,其中就有任务等待执行相关队列,并且有各个任务有着自己优先级,以便确定调度执行具体任务,如果你学过操作系统相关知识,那么应该对系统调度有所了解。

当一个作业被完成或者被中断后,调度器会调用

math?formula=EXTRACT-MAX%20(S)%20来调用所有在队列中等待任务中优先级最高的任务执行,在新任务加入等待任务时会调用

math?formula=INSERT(S%2C%20x)%20加入任务等待队列,当某个任务等待时间过长时可通过

math?formula=INCREASE-KEY(S%2C%20x%2Ckey)%20提高其优先级,从而减少等待时间。

下面是具体实现C程序源码:

#include

#define NAGE_INFINIT -99999

#define parent(i) i/2

#define left(i) 2*i+1

#define right(i) 2*i+2

//get array of A first element

int heap_maximum(int A[]){ return A[0];}

/***********************************************

*

* function max_heapify();

*

* args

*  A[] inttype save elements of heap

*  i index of A

*  heap_size real length of A

*

* ********************************************/

void max_heapify(int A[],int i,int heap_size){

int l,r,largest,temp;

l=left(i);

r=right(i);

if((l<=heap_size)&&(A[l]>A[i]))

largest=l;

else

largest=i;

if((r<=heap_size)&&(A[r]>A[largest]))

largest=r;

if(largest!=i){

temp=A[i];

A[i]=A[largest];

A[largest]=temp;

max_heapify(A,largest,heap_size);

}

}

/*********************************************

*

* function heap_extract_max()

*

* args

*  A[] inttype save elements of heap

*  heap_size inttype the real length of A

*

* return max the parent node value

*

* ******************************************/

int heap_extract_max(int A[],int heap_size){

int max;

if(heap_size<0)

return -1;  //heap underflow

max=A[0];  //parent node the max value of element

A[0]=A[heap_size];

heap_size--;

/**************************************

* dajust binary heap(or tree) to make

* sure heap fo A true every times

*

* ************************************/

max_heapify(A,0,heap_size);

return max;

}

/***********************************************

*

* function heap_increase_key();

*

* args

*  A[] inttype save elemnts of heap

*  i index of A

*  key inserted element

*

* *********************************************/

void heap_increase_key(int A[],int i,int key){

int temp;

if(key

printf("new key is smaller than current key\n");

return;    //over programming

}

A[i]=key;

//p=parent(i);

while ((i>0)&&(A[parent(i)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值