已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
程序代码:#include#include#define MAXMSIZE 99using namespace std;struct Nodeint startAddress;char name;int size;int endAddress;struct Node *prior;struct Node *next;struct Blocklist Node* head;Blocklist* freelist=new Blocklist;Blocklist* busylist=new Blocklist;void initial();void allocateBlock();void print();void reclaimBlock();void main()int slct; initial();print(); coutselect allocetion (1) or reclaim (2):slct;switch( slct )case 1: allocateBlock(); print(); break;case 2: reclaimBlock();print(); break;default: break;void allocateBlock() /分配分区char nm;int strtadd,sz; Node *ftemp,*delt,*btemp;ftemp=freelist-head ;btemp=busylist-head ;coutplseae input a new block information: endl;cout -name-startAdd-size- nmstrtaddsz;bool bfinish=false;bool ffinish=false;Node* p=new Node;p-name =nm;p-startAddress =strtadd;p-size =sz;p-endAddress =p-startAddress +p-size-1 ;if( busylist-head =NULL & bfinish =false) /分配使用链表的头结点btemp =p;btemp-prior =NULL;btemp-next =NULL;bfinish =true;busylist-head =btemp;elsewhile( btemp !=NULL & bfinish=false) /查找使用链表的可分配点if( btemp-endAddress startAddress & btemp-next =NULL) /加入使用链表链尾p-next =NULL;p-prior =btemp;btemp-next =p;bfinish =true;else if(btemp-endAddress startAddress & btemp-next-startAddress p-endAddress ) /加入使用链表链中p-next = btemp-next ; p-prior =btemp; btemp-next-prior =p;btemp-next =p;bfinish =true; else if( btemp-prior =NULL & btemp-startAddress p-endAddress & p-startAddress =0) /加入使用链表链头p-next =btemp;p-prior =NULL;btemp-prior =p;busylist-head =p;bfinish =true;btemp=btemp-next ; while( ftemp!=NULL & ffinish=false & bfinish =true ) /增加结点修改空闲链表 if( ftemp-startAddress startAddress & ftemp-endAddress =p-endAddress )Node* q=new Node;q-startAddress =p-endAddress +1;q-endAddress =ftemp-endAddress;q-size =q-endAddress- q-startAddress +1;q-prior =ftemp;q-next =ftemp-next ; ftemp-endAddress =p-startAddress-1;ftemp-size =ftemp-endAddress -ftemp-startAddress +1 ;ftemp-next =q ;ffinish =true;ftemp= ftemp-next; if( ffinish=true) /查找需要删除的结点并删除之 ftemp =freelist-head ; while( ftemp!=NULL) if( ftemp-size=0) if( ftemp = freelist-head ) freelist-head =ftemp-next ; else ftemp-prior -next =ftemp-next ; ftemp-next -prior =ftemp-prior ; delt =ftemp; ftemp =ftemp-next ; delete delt; else ftemp =ftemp-next ;coutit has been allocated !endl;elsecoutit cannot allocate!endl;void initial() /初始分配coutinitial. startAddress =0;fl-endAddress =MAXMSIZE;fl-size =fl-endAddress-fl-startAddress+1 ;fl-prior =NULL;fl-next =NULL;freelist-head =fl; busylist-head =NULL;void print() /打印执行结果Node* ftemp=freelist-head;Node* btemp=busylist-head ;coutfreelist information-startadd-endadd-size-endl;while( ftemp!=NULL )cout -startAddress - endAddress -size next ;coutbusylist information-name-startadd-endadd-size-endl;while( btemp!=NULL )cout -name -startAddress -endAddress-size next ; couthead ;btemp=busylist-head ;coutplease input the name of reclaimed block:nm; while( btemp!=NULL) / 确定要回收的内存空间if( btemp-name=nm) break;btemp =btemp-next; while( ftemp!=NULL) / 回收选定的内存空间 if( btemp-endAddress head-startAddress ) / 要回收的内存在空闲链表之前if( ftemp-startAddress = btemp-endAddress+1)ftemp-startAddress = btemp-startAddress ;ftemp-size +=btemp-size ;ftemp-prior =NULL;else busylist-head =btemp-next ;if(busylist-head) busylist-head -prior =NULL; btemp-next =freelist-head ; btemp-prior =NULL; freelist-head -prior =btemp; freelist-head =btemp; freelist-head -prior =NULL; finish =true;else if( btemp-startAddress ftemp-endAddress & btemp-endAddress next -startAddress ) /要回收的内存在空闲链表之中if( btemp-startAddress =ftemp-endAddress +1 & btemp-endAddress =ftemp-next-startAddress -1)/上下皆邻ftemp-size = ftemp-size +btemp-size +ftemp-next-size;ftemp-endAddress =ftemp-startAddress +ftemp-size -1;if ( ftemp-next -next)ftemp-next-next-prior =ftemp;ftemp-next = ftemp-next -next ;else if(btemp-startAddress =ftemp-endAddress +1) /上邻 ftemp-size +=btemp-size ;ftemp-endAddress =ftemp-startAddress +ftemp-size -1; else if(btemp-endAddress =ftemp-next-startAddress -1) /下邻ftemp-next-startAddress =btemp-startAddress ;ftemp-size =ftemp-next -endAddress -ftemp-next -startAddress +1;else /上下都不相邻btemp-next =ftemp-next ;ftemp-next -prior =btemp;ftemp-next =btemp;btemp-prior =ftemp;ftemp=ftemp-next ; if( !btemp-prior & finish=false )busylist-head =btemp-next ; if(busylist-head)busylist-head -prior =NULL;else if( finish=false)if( btemp-next )btemp-next -prior =btemp-prior;btemp-prior-next =btemp-next ;运行结果:initial.freelist information-startadd-endadd-size- -0 - 99-100busylist information-name-startadd-endadd-size-select allocetion (1) or reclaim (2):1plseae input a new block information: -name-startAdd-size-a 20 25it has been allocated !freelist information-startadd-endadd-size- -0 - 19-20 -45 - 99-55busylist information-name-startadd-endadd-size- -a-20-44-251plseae input a new block information: -name-startAdd-size-b 48 20it has been allocated !freelist information-startadd-endadd-size- -0 - 19-20 -45 - 47-3 -68 - 99-32busylist information-name-startadd-endadd-size- -a-20-44-25 -b-48-67-201plseae input a new block information: -name-startAdd-size-c 70 10it has been allocated !freelist information-startadd-endadd-size- -0 - 19-20 -45 - 47-3 -68 - 69-2 -80 - 99-20bus
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025上海新途社区健康促进社招聘健康社工实习生8人备考题库附答案详解(黄金题型)
- 2025年普洱市消防救援支队专职消防员招聘备考题库(4人)含答案详解(研优卷)
- 2025北京大兴区第九批公益性岗位招聘15人备考题库及答案详解(夺冠)
- 2025年嘉兴海宁市潮富人力资源服务有限公司招聘1人备考题库含答案详解(a卷)
- 2025渤海银行管理人员市场化选聘招聘备考题库及1套参考答案详解
- 2026重庆三峡银行校园招聘104人备考题库及一套完整答案详解
- 个性化社区慢病健康促进方案
- 2025阳泉平定县从社区专职网格员中选聘社区专职工作人员备考题库附答案详解(能力提升)
- 2025广西防城港市东兴边检招聘辅警15人备考题库含答案详解(模拟题)
- 2025海南东方市总工会招聘社会化工会工作者3人备考题库及完整答案详解
- 扑克牌搭高塔 课件(16张PPT) 小学班会活动
- 2023学年完整公开课版插座安装
- 贵州大型图书馆工程监理大纲鲁班奖
- 医疗机构麻醉药品和精神药品的使用管理
- GB/T 3672.1-2002橡胶制品的公差第1部分:尺寸公差
- GB/T 26746-2011矿物棉喷涂绝热层
- 【公开课课件】大班科学垃圾找家
- 第15课《故乡》说课课件(共19张ppt) 部编版语文九年级上册
- 黑龙江八一农垦大学-渠文军-答辩通用PPT模板
- 李嘉诚推举之文盔甲骑士
- 风机基础施工技术讲义方案课件
评论
0/150
提交评论