下载
加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 应用堆实现优先队列

应用堆实现优先队列.doc

应用堆实现优先队列

飘着白云的蓝天
2017-10-25 0人阅读 举报 0 0 暂无简介

简介:本文档为《应用堆实现优先队列doc》,可适用于IT/计算机领域

应用堆实现优先队列#include<IOSTREAM>usingnamespacestd#defineMSintmaxsize=,posintqueueMSÊýݺÊývoidSwap(inta,intb){inttt=queueaqueuea=queuebqueueb=t}µØ×îóÖµºÊýËùµÄÎÖÃintMaxEle(inta,intb){if(queuea>queueb)returnaelsereturnb}ÒÔlowΪùµÄÊÀíΪóùÑvoidHeapify(intlow){if(low*<=maxsize){if(queuelow<QUEUEMAXELE(LOW*,if(queuelow*{low*))>=queuelow*){Swap(low,low*)Heapify(low*)}else{Swap(low,low*)Heapify(low*)}}elsereturn}elseif(low*<=maxsize){if(queuelow*>queuelow){Swap(low,low*)Heapify(low*)}elsereturn}elsereturn}óùÑvoidbuildHeap(){intifor(i=maxsizei>=i){Heapify(i)}}õÊÑvoidInit(){intiprintf("ÇëÊäÈëÄãÒªÊäÈëµÄÔªËØöÊýº")scanf("d",maxsize)printf("ÇëÊäÈëdöÔªËØn",maxsize)for(i=i<=maxsizei){scanf("d",queuei)}buildHeap()}ÈëÓÅÏÈÓÁÐvoidPush(intele){inttif(maxsize==MS){printf("Error!n")}else{maxsizequeuemaxsize=elet=maxsizewhile(t>queuet>queuet){Swap(t,t)t=t}}}öÓÅÏÈÓÁÐÔªËØæµeleÖÐÊäÔÓÈlog(n)intPop(){intansif(maxsize<){printf("Error!n")return}else{Swap(,maxsize)maxsizeHeapify()returnqueuemaxsize}}ÑÅÅÐòÕÕÓеóµÄËÐòÅÅÐòvoidHeapSort(){intiintt=maxsizefor(i=maxsizei>=i){Swap(,i)maxsizeHeapify()}maxsize=t}µØÑÖÐÔªËØöÊývoidheapSize(){printf("ÑÖÐÔªËØöÊýΪºdn",maxsize)}µØÑÖÐÔªËØiÊÇñΪÒ×ÓáµãboolisLeaf(inti){if(i>=maxsizei<=maxsize)returntrueelsereturnfalse}µØÑÖÐiáµãµÄ×óù×ÓvoidleftChild(inti){if(i>=maxsize){printf("Error!n")}else{printf("áµãdµÄ×óù×ÓµÄֵΪdn",i,queuei*)}}µÝééÕÒÓÅÏÈΪkeyµÄÔªËØvoidFindEle(intkey,inti){if(i>maxsize)returnif(key>queuei)returnelse{if(key==queuei){pos=ireturn}else{FindEle(key,*i)FindEle(key,*i)}}}éÕÒÓÅÏÈΪkeyµÄÔªËصØÔªËصÄÎÖÃÃÓÐÃkeyÔòµØintisFind(intkey){pos=FindEle(key,)returnpos}ÊäöÑÖÐÔªËØvoidPrint(){intifor(i=i<=maxsizei){printf("d",queuei)}printf("n")}ÄäÓÅÏȺÊýÎÖÆäóùÑÐÔÖÊvoidChange(intkey){pos=isFind(key)if(pos==){printf("ÃÓÐÕÒµÃkeyÖµµÄÔªËØn")return}printf("ÌæÇn")Print()printf("ÌæÇn")printf("ÇëÊäÈëÌæµÄkeyÖµº")scanf("d",key)queuepos=keyüĵÄáµãΪùáµãif(pos==)Heapify()üĵÄáµãÓÅÏÈóÓÚÚµãelseif(queuepos>queuepos){while(pos!=queuepos>queuepos){Swap(pos,pos)pos=}}üĵÄáµãÓÅÏÈÐÓÚÚµãelseif(queuepos<queuepos){Heapify(pos)}printf("Ìæºón")Print()printf("Ìæºón")}˵ÏîvoidMenu(){printf("n")printf("øÓÁÐn")printf("öÓÁÐn")printf("µØÑÖеÇÔªËصÄöÊýn")printf("áµãiÊÇñÊÇÒöÒáµãn")printf("µØÑÖÐáµãiµÄ×óù×ÓµÄÎÖÃn")printf("ÔÑÖÐÔªËØÅÅÐòn")printf("üÄiºÅÔªËصÄÓÅÏÈΪkeyn")printf("õÊÑõÊÊýÝn")printf("ÊäöÑÖеÄÔªËØn")printf("ÍËön")}ÖºÊýintmain(){boolmark=trueinti,key,op,iowhile(mark){Menu()scanf("d",op)switch(op){case:mark=falsebreakcase:printf("µöÔªËØÈëÓÁÐn")printf("àöÔªËØÈëÓÁÐn")scanf("d",io)switch(io){case:printf("ÇëÊäÈëÈëÓÁеÄÔªËØÖµn")scanf("d",i)Push(i)breakcase:printf("ÊäÈëÖÕÖÈëÓÁÐn")while(scanf("d",i)i!=)Push(i)break}breakcase:printf("µöÔªËØöÓÁÐn")printf("ËùÓÐÔªËØöÓÁÐn")scanf("d",io)switch(io){case:i=Pop()if(i!=)printf("öÓÁеÄÔªËØֵΪºdn",i)breakcase:printf("öÓÁеÄÔªËØֵΪº")while(maxsize)printf("d",Pop())break}breakcase:heapSize()breakcase:printf("ÇëÊäÈëáµãiÐÓÚdn",maxsize)scanf("d",i)if(isLeaf(i))printf("áµãdÊÇÒáµãn",i)elseprintf("áµãdÊÇÒáµãn",i)breakcase:printf("ÇëÊäÈëáµãi,ÐÓÚdn",maxsize)scanf("d",i)if(i>maxsize)printf("Error!")elseleftChild(i)breakcase:printf("ÅÅÐòÇn")Print()printf("ÅÅÐòÇn")HeapSort()printf("ÅÅÐòºón")Print()printf("ÅÅÐòºón")breakcase:printf("ÇëÊäÈëÒªéÕÒµÄkeyÖµn")scanf("d",key)Change(key)breakcase:Init()breakcase:Print()break}}return}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/14

应用堆实现优先队列

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利