采用首次适应算法的动态分区分配模拟

来源:互联网 发布:服装销售网络渠道 编辑:程序博客网 时间:2024/05/16 00:47
#include <stdio.h>void hui(Fq *head){Fq *p=head->next;p->before=head;int no=0;cout<<"请输入要回收内存的作业号:";cin>>no;while(p!=NULL){if(p->no==no){free(p);cout<<"作业"<<no<<"的内存已经回收!"<<endl;return ;}else{p=p->next;}}}void alloc(int b,int no,Fq *p){while(p!=NULL){if(p->o==1){p=p->next;}else{if(p->size>b){if(p->size-b<=min_size){p->o=1;p->no=no;}else{Fq *q=new Fq;Fq *r;r=q->before;r->next=q;q->before=r;q->next=p;p->before=q;q->size=b;p->size=p->size-b;q->no=no;q->o=1;}}cout<<"内存分配成功!"<<endl;return ;}else{p=p->next;}}cout<<"内存分配失败!"<<endl;}void free(Fq *c){if(c->before->o==0&&c->next->o==0){Fq *r=c->before,*s=c->next->next;r->size=(r->size+c->size+c->next->size);r->next=s;if(s!=NULL)s->before=r;}if(c->before->o==0&&c->next->o!=0){c->before->size=c->before->size+c->size;c->beofre->next=c->next;c->next->before=c->before;}if(c->next->o==&&c->before->o!=0){Fq *r=c->next->next;c->size=c->size+c->next->size;c->next=c->next->next;if(r!=NULL)r->before=c;c->o=0;}else{c->o=0;}}void cha(Fq *head){Fq *p=head->next;p->before=head;int i=0;cout<<"空闲分区链的情况为:"<<endl;while(p!=NULL){if(p->o==0){cout<<"空闲分区"<<++i<<"  "<p-size<<"K"<<endl;}p=p->next;}}void create(Fq *head){Fq *p=head->next;p->before=head;int no=0,b=0;cout<<"请输入要创建的作业的编号:";cin>>no;cout<<"请输入作业的需求空间大小:";cin>>b;alloc(b,no,p);}int main(){Fq *heand=new Fq;head->next=new Fq;head->next->size=MAXSIZE;head->next->o=0;head->next->next=NULL;int choice=0;do{cout<<"请选择你要进行的操作:"<<endl;cout<<"1、创建新作业"<<endl;cout<<"2、查看空闲分区链"<<endl;cout<<"3、回收内存空间"<<endl;cout<<"0、退出"<<endl;cin>>choice;switch(choice){case 1:create(head);break;case 2:cha(head);break;case 3:hui(head);break;case 0:break;default:cout<<"输入错误!"<<endl;}while(choice!=0);}}

0 0
原创粉丝点击