最差适应法.docVIP

  • 4
  • 0
  • 约4.42千字
  • 约 10页
  • 2018-01-02 发布于河南
  • 举报
最差适应法

最差适应法 #include stdio.h #include malloc.h struct node { int start; /* 内存起始地址 */ int size; /* 内存大小 */ struct node *next; }; struct node *AMCB = NULL; /* 已分配内存区 */ struct node *FMCB = NULL; /* 空闲内存区 */ void PrintStatus() { int i; struct node *head; printf(=== 当前内存状态如下:===\n); printf(--- 已分配内存区 ---\n); head = AMCB; i = 1; while (head != NULL) { printf(No.%d: 起始地址 %4d 长度 %4d\n, i++, head-start, head-size); head = head-next; } printf(--- 空闲内存区 ---\n); head = FMCB; i = 1; while (head != NULL) { printf(No.%d: 起始地址 %4d 长度 %4d\n, i++, head-start, head-size); head = head-next; } } void InitData() { int i, start = 1; int size[] = {1000, 300, 600, 900, 700}; struct node *p1, *p2; for(i=0;i5;i++) { p1 = (struct node *)malloc(sizeof(struct node)); p1-start = start; p1-size = size[i]; p1-next = NULL; start += size[i]; if (i==0) { AMCB = p1; } else { p2-next = p1; } p2 = p1; } FMCB = (struct node *)malloc(sizeof(struct node)); FMCB-start = p2-start + p2-size; FMCB-size = 1500; FMCB-next = NULL; } void firstFit() { int op, size,flag=0,k=0,u=0; struct node *head, *p,*p1, *p2,*t2,*t1; struct node *q=NULL; /* 初始化数据 */ InitData(); /* 当前状态 */ PrintStatus(); printf(1:分配内存 2:回收内存 3:退出\n请输入操作数1-3: ); scanf(%d,op); while(op!=3) { if(op==1) { head=FMCB; printf(请输入分区大小:); scanf(%d,size); if((k=head-size-size)((head-size-size)=0))//头结点先算 { k=head-size-size; p1=head; flag=1; } for(head=FMCB;head-next !=NULL;head=head-next ) //找到比size大的最大空闲区域 { u=head-next -size -size; if(k=uu=0) {k=u; p1=head-next ; p2=head; flag=2;} } if(flag==0) printf(分配失败,没有足够大的分块\n); else { p=(struct node *)malloc(sizeof(struct node)); //开辟新的结点 p-size=size; p-start=p1-start; p-next=NULL; if(flag==1)//合适的空闲块在空闲区链表表头 { FMCB-start=p1-start+size; FMCB-size =p1-

文档评论(0)

1亿VIP精品文档

相关文档