代码存在尚一些问题:就是默认输入是按顺序的,所以开始没有排序模块;还有一个是关于指针修改值的问题,问题只有出现空闲区或者已分配区在执行过程中被清零会显现出来…;
扩展:
最优适应分配算法:只需要加一个排序,每次对剩余分区大小从小到大排序,然后遍历选择最小的;
最坏适应分配算法相反;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//空闲区
typedef struct free
{
int begin; //作业存放区开始地址
int lenght; //作业长度
struct free* next;
}Free;
//已分配区
typedef struct busy
{
char Jname[6]; //作业名
int begin; //作业存放区开始地址
int lenght; //作业长度
struct free* next;
}Busy;
//作业分配,采用最先适应分配算法
Free* allocate(Free *ftable,Busy *btable)
{
int i;
int lenght;
char name[6];
Free *h = NULL,*p = NULL;
h = p = ftable;
Busy *last = btable;
Busy *cur = NULL;
printf("\n请输入申请空间及作业名\n");
scanf("%d",&lenght);
scanf("%s",name);
while(h){
if(h->lenght < lenght){ //寻找满足申请的空间