- 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-
您可能关注的文档
最近下载
- LaserjetCP1025系列打印机打印质量故障排除手册.pdf VIP
- 年会小品剧本小品剧本:搞笑小品剧本《都是喝酒惹事》台词大全.docx VIP
- 2026年高考全国II卷文科综合真题试卷(新课标卷)(+答案).docx VIP
- 安路科技(688107)补国产短板,突高端应用,拓新应用与技术大市场-240717-东北证券-38页.pdf VIP
- [搞笑小品剧本]上当了小品剧本.docx VIP
- 数据中心800V直流供电技术白皮书2.0.pdf
- 上海大学2022-2023学年第1学期《高等数学(上)》期末考试试卷(B卷)附参考答案.pdf
- 上海大学2022-2023学年第1学期《高等数学(上)》期末考试试卷(A卷)附参考答案.pdf
- 中国教育行业人才流动与薪酬水平_2025年12月.docx
- 2024全国初中数学联赛初二卷 .pdf VIP
原创力文档

文档评论(0)