最佳适应(BestFit)算法

基本思想:BF的空闲分区表(或空闲分区链)按空闲分区大小的升序方式组织。分配时,按空闲分区表(或空闲分区链)的先后次序,从头查找,找到符合要求的第一个分区。就说明它是最适合的。大的空闲分区可以被保留下来

#include <malloc.h> 
#include <stdio.h> 
#include <string.h> 
#define NULL 0 
typedef struct table 
{
    int address;     /*存储分区起始地址*/ 
    int length;      /*存储分区长度*/ 
    int flag;        /*存储分区标志,0 为空闲,1 为被作业占据*/ 
    char name[10];   /*当flag==1 时存储分区占用标志作业名,否则存储空nil*/ 
    struct table *next; 
}node; 

int success=0;  /*分配成功与否的标志*/ 

node *insert(node *head, node *p)  /*按照“地址递增方式”将p 结点插入链表相应位置 参考优先级*/ 
  { 
    node *r,*q;
    r=head;
    if(head->flag)
{
    if(r->address>p->address)
    {
        p->next=r;
        return p;
    }
    else
    {
        while(r->address<p->address)
        {
             if(r->next==NULL)
             {
                 r->next=p;
                 return head;
             }
             q=r;
             r=r->next;
        }
    }
      q->next=p;
       p->next=r;
       return head;
}
    else
{
    if(r->length>p->length)
    {
        p->next=r;
        return p;
    }
    else
    {
        while(r->length<p->length)
        {
             if(r->next==NULL)
             {
                 r->next=p;
                 return head;
             }
             q=r;
             r=r->next;
        }
    }
      q->next=p;
       p->next=r;
       return head;
    }

}

node *creat()   /*根据地址递增方式建立分配分区表(flag==1)或空闲分区表(flag==0)*/ 
{ 
   node *head,*p1;
   int n=0; 
   char a[10]="nil";
   head=NULL;
   //建立分配分区表
   printf("address  length  flag<0 or 1>\n");
   p1=(node *)malloc(sizeof(node));
   scanf("%d %d %d",&p1->address,&p1->length,&p1->flag);
   while(p1->length!=0||p1->address!=0)
    {
        if(p1->flag)
   {
       printf("\t\tinput job_name:");
        scanf("%s",&p1->name);
   }
   else
   {
       strcpy(p1->name, "nil"); 
   }
   p1->next=NULL;
       n=n+1;
   if (n==1) {head=p1;p1->next=NULL;}
     else head=insert(head,p1);
     p1=(node *) malloc (sizeof(node));
   scanf("%d",&p1->address);
   scanf("%d",&p1->length);
   scanf("%d",&p1->flag);

    }
   return head;
} 
void print (node *head) /*输出链表*/ 
{
    node *p;
    p=head;
    if(p->flag){
        printf("The distributed table is !\n");
    }
    else{
        printf("The free table is !\n");
    }

    if(!p)
    {
        printf("该链表为空!\n");
    }
    else{
        while(p)
        {
        printf("%d,%d,%d,%s\n",p->address,p->length,p->flag,p->name);
        p=p->next;
        }
        printf("\n");
    }
} 
void distribute(node *freehead, node *distributedhead, node *work) 
/*在空闲分区表中找出首次合适work 的分区,同时修改空闲分区表和分配分区表*/ 
{ //填补程序
 node *p,*q,*r,*t,*news;
 int a,b,c,d;
  p=freehead;
  q=distributedhead;
  while(p)
  {
      if(p->length>=work->length)
      {
        d=p->address;
        p->address=p->address+work->length;
        p->length=p->length-work->length;
        if(p->length==0)
        {
            t->next=p->next;
        }
        success=1;
        r=t;
        break;
      }
      else
      {
          t=p;
          p=p->next;
      }
  }
  if(success)
  {
      while(q)
      {
          a=q->address;
          b=q->length;
          c=a+b;
        if(c==d)
        {
      news=(node *)malloc(sizeof(node));
      news->address=c;
      news->length=work->length;
      news->flag=1;
      printf("\ndistributing is successful!\n");
       strcpy(news->name, work->name); 
      news->next=q->next;
      q->next=news;
      print(freehead);
      print(distributedhead);
      return;
         }
       q=q->next;
      }
  }
  else{
     printf("\ndistributing is not successful!\n");
      print(freehead);
      print(distributedhead);
  }
} 


void main() 
 { 

    struct table *dtable,*ftable,*work; 
    char workn[10]; 
    int a; 
    printf("\nThe distributed table is:\n"); 
    dtable=creat();
//  print(dtable);
        /*dtable 输入已分配情况表*/ 
    printf("\nThe free table is:\n"); 
    ftable=creat();          
//  print(ftable);/*ftable 输入未分配情况表*/ 
     while(1)
     {
         work=(node *)malloc(sizeof(node));
         printf("\nThe length of worked job is:");
         scanf("%d",&work->length);
         printf("The name of worked is:");
         scanf("%s",&workn);
         strcpy(work->name, workn); 
         distribute(ftable,dtable,work);
     } 
 } 
  • 0
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值