首次适应算法-最佳适应算法-最坏适应算法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdlib.h#defineFree0//¿ÕÏÐ״̬#defineBusy1//ÒÑÓÃ״̬#defineOK1//Íê³É#defineERROR0//³ö´í#defineMAX_length640//×î´óÄÚ´æ¿Õ¼äΪ640KBtypedefintStatus;intflag;typedefstructfreearea//¶¨ÒåÒ»¸ö¿ÕÏÐÇø˵Ã÷±í½á¹¹{longsize;//·ÖÇø´óСlongaddress;//·ÖÇøµØÖ·intstate;//״̬}ElemType;//ÏßÐÔ±íµÄË«ÏòÁ´±í´æ´¢½á¹¹typedefstructDuLNode{ElemTypedata;structDuLNode*prior;//Ç°Ç÷Ö¸ÕëstructDuLNode*next;//ºó¼ÌÖ¸Õë}DuLNode,*DuLinkList;DuLinkListblock_first;//Í·½áµãDuLinkListblock_last;//β½áµãStatusalloc(int);//ÄÚ´æ·ÖÅäStatusfree(int);//ÄÚ´æ»ØÊÕStatusFirst_fit(int);//Ê×´ÎÊÊÓ¦Ëã·¨StatusBest_fit(int);//×î¼ÑÊÊÓ¦Ëã·¨StatusWorst_fit(int);//×î²îÊÊÓ¦Ëã·¨voidshow();//²é¿´·ÖÅäStatusInitblock();//¿ª´´¿Õ¼ä±íStatusInitblock()//¿ª´´´øÍ·½áµãµÄÄÚ´æ¿Õ¼äÁ´±í{block_first=(DuLinkList)malloc(sizeof(DuLNode));block_last=(DuLinkList)malloc(sizeof(DuLNode));block_first-prior=NULL;block_first-next=block_last;block_last-prior=block_first;block_last-next=NULL;block_last-data.address=0;block_last-data.size=MAX_length;block_last-data.state=Free;returnOK;}//·ÖÅäÖ÷´æStatusalloc(intch){intrequest=0;coutÇëÊäÈëÐèÒª·ÖÅäµÄÖ÷´æ´óС(µ¥Î»:KB)£º;cinrequest;if(request0||request==0){cout·ÖÅä´óС²»ºÏÊÊ£¬ÇëÖØÊÔ£¡endl;returnERROR;}if(ch==2)//Ñ¡Ôñ×î¼ÑÊÊÓ¦Ëã·¨{if(Best_fit(request)==OK)cout·ÖÅä³É¹¦£¡endl;elsecoutÄÚ´æ²»×㣬·ÖÅäʧ°Ü£¡endl;returnOK;}if(ch==3)//Ñ¡Ôñ×î²îÊÊÓ¦Ëã·¨{if(Worst_fit(request)==OK)cout·ÖÅä³É¹¦£¡endl;elsecoutÄÚ´æ²»×㣬·ÖÅäʧ°Ü£¡endl;returnOK;}else//ĬÈÏÊ×´ÎÊÊÓ¦Ëã·¨{if(First_fit(request)==OK)cout·ÖÅä³É¹¦£¡endl;elsecoutÄÚ´æ²»×㣬·ÖÅäʧ°Ü£¡endl;returnOK;}}//Ê×´ÎÊÊÓ¦Ëã·¨StatusFirst_fit(intrequest){//ΪÉêÇë×÷Òµ¿ª±ÙпռäÇÒ³õʼ»¯DuLinkListtemp=(DuLinkList)malloc(sizeof(DuLNode));temp-data.size=request;temp-data.state=Busy;DuLNode*p=block_first-next;while(p){if(p-data.state==Free&&p-data.size==request){//ÓдóСǡºÃºÏÊʵĿÕÏпép-data.state=Busy;returnOK;break;}if(p-data.state==Free&&p-data.sizerequest){//ÓпÕÏпéÄÜÂú×ãÐèÇóÇÒÓÐÊ£Óàtemp-prior=p-prior;temp-next=p;temp-data.address=p-data.address;p-prior-next=temp;p-prior=temp;p-data.address=temp-data.address+temp-data.size;p-data.size-=request;returnOK;break;}p=p-next;}returnERROR;}//×î¼ÑÊÊÓ¦Ëã·¨StatusBest_fit(intrequest){intch;//¼Ç¼×îСʣÓà¿Õ¼äDuLinkListtemp=(DuLinkList)malloc(sizeof(DuLNode));temp-data.size=request;temp-data.state=Busy;DuLNode*p=block_first-next;DuLNode*q=NULL;//¼Ç¼×î¼Ñ²åÈëλÖÃwhile(p)//³õʼ»¯×îС¿Õ¼äºÍ×î¼ÑλÖÃ{if(p-data.state==Free&&(p-data.size=request)){if(q==NULL){q=p;ch=p-data.size-request;}elseif(q-data.sizep-data.size){q=p;ch=p-data.size-request;}}p=p-next;}if(q==NULL)returnERROR;//ûÓÐÕÒµ½¿ÕÏпéelseif(q-data.size==request){q-data.state=Busy;returnOK;}else{temp-prior=q-prior;temp-next=q;temp-data.address=q-data.address;q-prior-next=temp;q-prior=temp;q-data.address+=request;q-data.size=ch;returnOK;}returnOK;}//×î²îÊÊÓ¦Ëã·¨StatusWorst_fit(intrequest){intch;//¼Ç¼×î´óÊ£Óà¿Õ¼äDuLinkListtemp=(DuLinkList)malloc(sizeof(DuLNode));temp-data.size=request;temp-data.state=Busy;DuLNode*p=block_first-next;DuLNode*q=NULL;//¼Ç¼×î¼Ñ²åÈëλÖÃwhile(p)//³õʼ»¯×î´ó¿Õ¼äºÍ×î¼ÑλÖÃ{if(p-data.state==Free&&(p-data.size=request)){if(q==NULL){q=p;ch=p-data.size-request;}elseif(q-data.sizep-data.size){q=p;ch=p-data.size-request;}}p=p-next;}if(q==NULL)returnERROR;//ûÓÐÕÒµ½¿ÕÏпéelseif(q-data.size==request){q-data.state=Busy;returnOK;}else{temp-prior=q-prior;temp-next=q;temp-data.address=q-data.address;q-prior-next=temp;q-prior=temp;q-data.address+=request;q-data.size=ch;returnOK;}returnOK;}//Ö÷´æ»ØÊÕStatusfree(intflag){DuLNode*p=block_first;for(inti=0;i=flag;i++)if(p!=NULL)p=p-next;elsereturnERROR;p-data.state=Free;if(p-prior!=block_first&&p-prior-data.state==Free)//ÓëÇ°ÃæµÄ¿ÕÏпéÏàÁ¬{p-prior-data.size+=p-data.size;p-prior-next=p-next;p-next-prior=p-prior;p=p-prior;}if(p-next!=block_last&&p-next-data.state==Free)//ÓëºóÃæµÄ¿ÕÏпéÏàÁ¬{p-data.size+=p-next-data.size;p-next-next-prior=p;p-next=p-next-next;}if(p-next==block_last&&p-next-data.state==Free)//Óë×îºóµÄ¿ÕÏпéÏàÁ¬{p-data.size+=p-next-data.size;p-next=NULL;}returnOK;}//ÏÔʾÖ÷´æ·ÖÅäÇé¿övoidshow(){intflag=0;cout\nÖ÷´æ·ÖÅäÇé¿ö:\n;cout++++++++++++++++++++++++++++++++++++++++++++++\n\n;DuLNode*p=block_first-next;cout·ÖÇøºÅ\tÆðʼµØÖ·\t·ÖÇø´óС\t״̬\n\n;while(p){coutflag++\t;coutp-data.address\t\t;coutp-data.sizeKB\t\t;if(p-data.state==Free)cout¿ÕÏÐ\n\n;elsecoutÒÑ·ÖÅä\n\n;p=p-next;}cout++++++++++++++++++++++++++++++++++++++++++++++\n\n;}//Ö÷º¯Êývoidmain(){intch;//Ë㷨ѡÔñ±ê¼ÇcoutÇëÊäÈëËùʹÓõÄÄÚ´æ·ÖÅäËã·¨£º\n;cout(1)Ê×´ÎÊÊÓ¦Ëã·¨\n(2)×î¼ÑÊÊÓ¦Ëã·¨\n(3)×î²îÊÊÓ¦Ëã·¨\n;cinch;while(ch1||ch3){coutÊäÈë´íÎó£¬ÇëÖØÐÂÊäÈëËùʹÓõÄÄÚ´æ·ÖÅäËã·¨£º\n;cinch;}Initblock();//¿ª´´¿Õ¼ä±íintchoice;//²Ù×÷Ñ¡Ôñ±ê¼Çwhile(1){show();coutÇëÊäÈëÄúµÄ²Ù×÷£º;cout\n1:·ÖÅäÄÚ´æ\n2:»ØÊÕÄÚ´æ\n0:Í˳ö\n;cinchoice;if(choice==1)alloc(ch);//·ÖÅäÄÚ´æelseif(choice==2)//ÄÚ´æ»ØÊÕ{intflag;coutÇëÊäÈëÄúÒªÊͷŵķÖÇøºÅ£º;cinfl

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功