#include<iostream>
#define memsize 1024
using namespace std;
typedef struct space{
int startad;
int endad;
int ID;
space *next;
}space;
void job();
void showjob();
void showmem();
void recyclejob();
void menu();
space *freehead,*freetag,*usinghead,*usingtag,*newjob;
void main(){
int i;
freehead=new space;
freehead->startad=0;
freehead->endad=memsize;
freehead->next=NULL;
freetag=freehead;
usinghead=usingtag=NULL;
cout<<"/t##### 计06-6班 周飞 ####"<<endl<<endl;
L:menu();
cin>>i;
switch (i){
case 1:showjob();break;
case 2:showmem();break;
case 3:recyclejob();break;
case 4:job();break;};
goto L;
}
void menu(){
cout<<"/t1.显示作业信息"<<endl;
cout<<"/t2.显示内存信息"<<endl;
cout<<"/t3.回收作业"<<endl;
cout<<"/t4.创建工作"<<endl;
};
void job(){
int size,ID;
cout<<"/t输入装载作业的大小和ID"<<endl;
cin>>size>>ID;
space *searchfree=freehead;
while((searchfree->endad)-(searchfree->startad)<size)
{
if(searchfree->next!=NULL) searchfree=searchfree->next;
else {cout<<"/t内存分配出错请回收作业以释放内存"<<endl;}
};
newjob=new space;
newjob->ID=ID;
newjob->startad=searchfree->startad;
newjob->endad=(newjob->startad)+size;
newjob->next=NULL;
searchfree->startad=(searchfree->startad)+size;
if(usinghead==NULL&&usingtag==NULL) usinghead=usingtag=newjob;
else {usingtag->next=newjob;usingtag=newjob;};
cout<<"/t作业已成功装入内存!!"<<endl<<endl;
};
void showjob(){
cout<<"/tID/t"<<"size/t"<<"startad/t/t"<<"endad/t"<<endl;
space *searchusing;
for(searchusing=usinghead;searchusing!=NULL;searchusing=searchusing->next)
{
cout<<"/t"<<searchusing->ID<<"/t"<<
(searchusing->endad)-(searchusing->startad)<<"/t"<<
searchusing->startad<<"/t/t"<<searchusing->endad<<endl;
};
};
void showmem(){
space *searchusing=usinghead;
space *searchfree=freehead;
showjob();
X: cout<<"/tNULL/t"<<(searchfree->endad)-(searchfree->startad)<<"/t"<<
searchfree->startad<<"/t/t"<<searchfree->endad<<endl;
searchfree=searchfree->next;
if (searchfree!=NULL) goto X;
};
void recyclejob(){
int ID;
int i=0;
space *searchfree=freehead;
space *searchusing=usinghead;
space *forward=new space;
forward->next=usinghead;
cout<<"/t输入作业ID"<<endl;
cin>>ID;
while(searchusing->ID!=ID){
searchusing=searchusing->next;
forward=forward->next;
}
forward->next=searchusing->next;
//更新加入
if (searchusing==usinghead) usinghead=searchusing->next;
else {if(searchusing==usingtag) usingtag=forward;}
//更新结束
//按内存顺序插入free链表排序,problemstart
if((freetag->startad)<(searchusing->startad)) { //判断队尾
freetag->next=searchusing;
freetag=searchusing;
searchusing->next=NULL;
}
if((freehead->startad)>(searchusing->startad)){ //判断对头
searchusing->next=freehead;
freehead=searchusing;
}
else {
while((searchusing->startad)>(searchfree->next->startad))
searchfree=searchfree->next;
searchusing->next=searchfree->next;
searchfree->next=searchusing;
};
//problemend
//合并相邻空闲分区
//调试freehead
searchfree=freehead;
while((searchfree->next)!=NULL)
{
if ((searchfree->endad)==(searchfree->next->startad))
{
(searchfree->endad)=(searchfree->next->endad);
(searchfree->next)=(searchfree->next->next);
}
else searchfree=(searchfree->next);
}
}