📄 osnew.cpp
字号:
#include<iostream.h>
#include<stdlib.h>
static address=0;
struct FreeList
{
//FreeList *h1;
FreeList *next;
int Freesize;//空闲区大小
int isempty;//判断空闲区接点是否为空.1-空,2-满
int Allocatesize;//分配区大小
int name;//作业名称
int start;//起始地址
};
struct ApplyList
{
//ApplyList *h2;
int name;//申请作业名称
int Applysize;//申请作业大小
int flag;
ApplyList *next;
};
FreeList *h1;
ApplyList *h2;
ApplyList *np;
//ApplyList *temp;
class M
{
public:
void setApplyList();
void setAllocateList(FreeList *,ApplyList *);
void DeleteProcess(FreeList *);
void showApplyList(ApplyList *);
void showAllocateList(FreeList *);
void showFreeList(FreeList *);
bool Find(FreeList *,int);
bool declare(ApplyList *,int);
};
void M::setAllocateList(FreeList *head1,ApplyList *cp)
{
FreeList *p=head1->next;
while(p)
{
if((p->Freesize>=cp->Applysize)&&(p->isempty==1))
{
if(p->next==NULL)
{
p->isempty=2;
p->Allocatesize=cp->Applysize;
p->name=cp->name;
p->start=address; /*静态全局变量address的初值是0*/
address+=cp->Applysize;
FreeList *newfree=new FreeList;
newfree->Freesize=p->Freesize-cp->Applysize;
newfree->isempty=1;
newfree->start=address;
newfree->next=NULL;
p->next=newfree;
cp->flag=2;
}
else
{
p->name=cp->name;
p->Allocatesize=cp->Applysize;
p->isempty=2;
p->start=address;
cp->flag=2;
}
break;
}
else /*申请区大于空闲区或空闲区结点不为空*/
{
p=p->next;
}
}
if(cp->flag==1)
cout<<"内存暂时不够,作业"<<cp->name<<"内存分配失败!!!"<<endl;
}
void M::setApplyList()//设置申请表
{
int name,Applysize;
char op='y';
while(op=='y')
{
cout<<"输入申请作业名称和大小:"<<endl;
cin>>name>>Applysize;
//if(declare(h2,name))
// continue;
ApplyList *cp;
cp=new ApplyList;
cp->name=name;
cp->Applysize=Applysize;
cp->flag=1;
np->next=cp;
np=cp;
//np->next=NULL;
setAllocateList(h1,cp);
cout<<"是否继续输入作业,y-是,任意键-否:";
cin>>op;
}
np->next=NULL;
}
bool M::declare(ApplyList *head,int name)/*判断申请的作业名是否已经存在*/
{
ApplyList *p=head->next;
while(p)
{
if(p->name==name)
{
cout<<"有同名作业存在于申请表中,请重新输入作业名称!";
return true;
}
p=p->next;
}
return false;
}
bool M::Find(FreeList *head,int name)//判断要删除的作业是否在分配表中
{
FreeList *p=head->next;
while(p)
{
if(p->isempty==2&&p->name==name)
return true;
p=p->next;
}
return false;
}
void M::DeleteProcess(FreeList *head)
{
FreeList *p=head->next;
FreeList *front=head;
FreeList *q;
char option='y';
int name;
while(option=='y')
{
cout<<"请从分配表中输入要删除的作业名称:"<<endl;
cin>>name;
if(!Find(head,name))//如果想要删除的作业不在分配表中,则重新输入
{
cout<<"你要删除的作业不在分配表中,请重新输入!"<<endl;
continue;
}
else
{
while(p->name!=name&&p!=NULL)/*往下查找,找到,p为止*/
{
front=p;
p=p->next;
}
q=p->next;
if(q==NULL)//要删除的是最后一个结点
{
if(front->isempty==1)
{
address-=p->Allocatesize;/*删除后,释放空闲区*/
front->Freesize+=p->Allocatesize;
front->next=NULL;
delete p;
}
else
{
address-=p->Allocatesize;
p->Freesize=p->Allocatesize;
p->isempty=1;
}
}
else if(p==head->next)//要删除的是第一个结点
{
if(p->next->isempty==1)/*如果下一段相邻内存是空闲区的话,*/
{ /*则把它与释放掉后的空闲区连起来*/
address-=p->Allocatesize;
p->Freesize=p->Allocatesize+p->next->Freesize;
p->isempty=1;
p->next=q->next;
delete q;
}
else
{
address-=p->Allocatesize;
p->Freesize=p->Allocatesize;
p->isempty=1;
}
}
else//要删除的是中间结点
{
if(front->isempty==1&&q->isempty==2)
{
address-=p->Allocatesize;
front->Freesize+=p->Allocatesize;
front->next=p->next;
delete p;
}
else if(front->isempty==2&&q->isempty==1)
{
address-=p->Allocatesize;
p->Freesize=p->Allocatesize+q->Freesize;
p->isempty=1;
p->next=q->next;
delete q;
}
else if(front->isempty==1&&q->isempty==1)
{
address-=p->Allocatesize;
front->Freesize+=p->Allocatesize+q->Freesize;
front->next=q->next;
delete p;
delete q;
}
else
{
address-=p->Allocatesize;
p->Freesize=p->Allocatesize;
p->isempty=1;
}
}
}
cout<<"是否继续删除作业,y-是,任意键-否:"<<endl;
cin>>option;
}
}
void M::showAllocateList(FreeList *head)//输出分配表的函数
{
FreeList *p=head->next;
cout<<"分配的作业名称,大小,地址为:"<<endl;
while(p)
{
if(p->isempty==2)
{
cout<<p->name<<" "<<p->Allocatesize<<" "<<p->start<<endl;
}
p=p->next;
}
}
void M::showApplyList(ApplyList *head)//输出申请表
{
ApplyList *p=head->next;
cout<<"申请的作业名称和大小:"<<endl;
while(p!=NULL)
{
cout<<p->name<<"\t"<<p->Applysize<<endl;
p=p->next;
}
}
void M::showFreeList(FreeList *head)//输出空闲表
{
FreeList *p=head;//->next;
cout<<"空闲区起始地址和大小:"<<endl;
while(p)
{
if(p->isempty==1)
cout<<p->start<<" "<<p->Freesize<<endl;
p=p->next;
}
}
void main()
{
h1=new FreeList;
FreeList *f=new FreeList;
f->Freesize=640;
f->Allocatesize=0;
f->isempty=1;
f->start=0;
f->next=NULL;
h1->next=f;
h2=new ApplyList;
h2->next=NULL;
np=h2;
int choosen;
M m;
char c='y';
while(c=='y')
{
cout<<"输入你的选择:1-输入,2-删除,3-退出";
cin>>choosen;
switch(choosen)
{
case 1:
m.setApplyList();
m.showApplyList(h2);
m.showAllocateList(h1);
m.showFreeList(h1);
break;
case 2:
m.DeleteProcess(h1);
m.showApplyList(h2);
m.showAllocateList(h1);
m.showFreeList(h1);
break;
case 3:
exit(1);
}
cout<<"是否继续,y-是,任意键-否:";
cin>>c;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -