break;
}
}
if((address+size)==point1->address)//如果与后一个相邻
{
point1->size=point1->size+size;//与后一个合并
break;
}
//如果前后都不相邻,申请新的节点
point2=new linknode(size,address,point1->forward,point1);
point1->forward->next=point2;
point1->forward=point2;
}
point1=point1->next;
}
if(point1==NULL)//
{
if((rear->address+rear->size)==address)//如果与rear相邻
{
rear->size=rear->size+size;
}
else
{ //另立新的rear
point2=new linknode(size,address,rear,NULL);
rear->next=point2;
rear=point2;
}
}
}
//判断是否输出完节点
bool first_fit_link::over()
{
{
work=head;
return true;
}
else
{
return false;
}
}
//返回链表元素
Job first_fit_link::returnjob()
{
linknode * point1=work;
Job pointNum;
if(point1!=NULL)
{
pointNum.size=point1->size;
}
work=work->next;
return pointNum;
}
#endif
最佳适应算法代码
#ifndef Bestfit_H
#define Bestfit_h
#include "Job.h"
//最佳适应算法头文件
class bestfit_link;
class bestnode
{
friend class bestfit_link;
private:
int size;//作业大小
int address;//作业首地址
bestnode * forward;
bestnode * next;
public:
bestnode(int s,int a,bestnode * f,bestnode * n)//初始化
{
size=s;
address=a;
forward=f;
next=n;
}
};
class bestfit_link
{
private:
bestnode * head;//头指针
bestnode * rear;//尾指针
bestnode * work;//标记指针
public:
bestfit_link()//初始化
{
head=NULL;
rear=NULL;
work=NULL;
}
~bestfit_link()//析构函数
{
bestnode * point1;
while(head!=NULL)
{
point1=head;
head=head->next;
delete point1;
}
}
Job returnjob();//返回链表元素
bool over();//判断是否输出完节点元素
int allot(int size);//分配
};
//分配函数,如果分配成功,返回作业地址,否则返回1000
int bestfit_link::allot(int size)
{
int address=1000;
bestnode * point1=head;
bestnode * point2;
while(point1!=NULL)
{
if(size<point1->size)//如果申请空间小于该节点,对该节点改动
{
address=point1->address;
point1->size=point1->size-size;
point1->address=point1->address+size;
return address;//分配成功,返回address
}
{
if(point1==head)//如果是头节点
{
address=head->address;
point2=head;