第一关
#include <stdio.h>
#include <stdlib.h>
const int Max_length=55;//最大内存
struct areaNode//管理分区的结构体
{
int ID;//分区号
int size;//分区大小
int address;//分区地址
int flag;//使用状态,0为未占用,1为已占用
};
typedef struct DuNode//双向链表结点
{
struct areaNode data;//数据域
struct DuNode *prior;//指针域
struct DuNode *next;
}*DuLinkList;
DuLinkList m_head = new DuNode, m_last = new DuNode;//双向链表首尾指针
void init()//分区链表初始化
{
m_head->prior = NULL;
m_head->next = m_last;
m_last->prior = m_head;
m_last->next = NULL;
m_head->data.size = 0;
m_last->data.address = 0;
m_last->data.size = Max_length;
m_last->data.ID = 0;
m_last->data.flag = 0;
}
void show()
{
DuNode *p = m_head->next;//指向空闲区队列的首地址
printf("+++++++++++++++++++++++++++++++++++++++\n");
while (p)
{
printf("分区号:");
if (p->data.ID == 0)
printf("FREE\n");
else
printf("%d\n",p->data.ID);
printf("分区起始地址:%d\n",p->data.address);
printf("分区大小:%d\n",p->data.size);
printf("分区状态:");
if (p->data.flag)
printf("已被占用\n");
else
printf("空闲\n");
printf("——————————————————\n");
p = p->next;
}
}
bool first_fit(int id, int m_size)//首次适应算法,id为作业号,m_size为作业大小
{
//请补充使用首次适应算法给作业分配内存的函数代码
DuLinkList p = new DuNode;
p->data.ID = id;
p->data.size = m_size;
p->data.address = (m_last->prior->data.address) + (m_last->prior->data.size);
m_last->data.size -= p->data.size;
if (m_last->data.size < 0)
return false;
p->next = m_last;
p->prior = m_last->prior;
m_last->prior->next = p;
m_last->prior = p;
m_last->data.address = (p->data.address) + (p->data.size);//更新m_last信息
p->data.flag = true;
return true;
}
void recycle(int id)//回收内存,id为释放内存的作业号
{
//请补充回收作业内存的代码
DuNode* f = m_head->next;
while (f->data.ID == id)
{
f = f->next;
}
if(f==nullptr)return;
DuNode* p = f->next;
p->prior = f->prior;
f->prior->next = p;
f->next = NULL;
while (p)
{
p->data.address = (p->prior->data.address) + (p->prior->data.size);
p = p->next;
}
m_last->data.size += f->data.size;
f->data.flag = false;
delete(f);
f = NULL;
}
int main()
{
init();
printf("首次适应算法:\n");
first_fit(1,15);
first_fit(2,30);
recycle(1);
first_fit(3,8);
first_fit(4,6);
recycle(2);
show();
DuNode *p = m_head;
while(p != NULL)
{
DuNode *temp = p;
p = p->next;
delete(temp);
temp = NULL;
}
return 0;
}
第二关
#include <stdio.h>
#include <stdlib.h>
const int Max_length=55;//最大内存
struct areaNode//管理分区的结构体
{
int ID;//分区号
int size;//分区大小
int address;//分区地址
int flag;//使用状态,0为未占用,1为已占用
};
typedef struct DuNode//双向链表结点
{
struct areaNode data;//数据域
struct DuNode *prior;//指针域
struct DuNode *next;
}*DuLinkList;
DuLinkList m_head = new DuNode, m_last = new DuNode;//双向链表首尾指针
void init()//分区链表初始化
{
m_head->prior = NULL;
m_head->next = m_last;
m_last->prior = m_head;
m_last->next = NULL;
m_head->data.size = 0;
m_last->data.address = 0;
m_last->data.size = Max_length;
m_last->data.ID = 0;
m_last->data.flag = 0;
}
void show()
{
DuNode *p = m_head->next;//指向空闲区队列的首地址
printf("+++++++++++++++++++++++++++++++++++++++\n");
while (p)
{
printf("分区号:");
if (p->data.ID == 0)
printf("FREE\n");
else
printf("%d\n",p->data.ID);
printf("分区起始地址:%d\n",p->data.address);
printf("分区大小:%d\n",p->data.size);
printf("分区状态:");
if (p->data.flag)
printf("已被占用\n");
else
printf("空闲\n");
printf("——————————————————\n");
p = p->next;
}
}
bool best_fit(int id, int m_size)//最佳适应算法,其中需要查找最佳的存放位置
{
//请补充使用最佳适应算法给作业分配内存的函数代码
DuNode* p = m_head->next;
DuNode* t = nullptr;
while (p)
{
if (p->data.flag == 0 && p->data.size >= m_size)
if (t == nullptr || t->data.size > p->data.size)
t = p;
p = p->next;
}
if (t == nullptr)
return false;
if (t->data.size == m_size)
{
t->data.flag = 1;
t->data.ID = id;
}
else {
DuNode* ans = new DuNode;
ans->prior = t->prior;
ans->next = t;
t->prior->next = ans;
t->prior = ans;
ans->data.ID = id;
ans->data.size = m_size;
ans->data.flag = 1;
ans->data.address = ans->prior->data.address + ans->prior->data.size;
ans->next->data.address = ans->data.address + ans->data.size;
ans->next->data.size -= ans->data.size;
}
return true;
}
void recycle(int id)//回收内存,id为释放内存的作业号
{
//请补充回收作业内存的函数代码
DuNode* f = m_head->next;
while (f->data.ID != id)
{
f = f->next;
}
if (f == nullptr)
return;
f->data.ID = 0;
f->data.flag = 0;
DuNode* fp = f->prior;
DuNode* fn = f->next;
if (fp != m_head && fp->data.flag == 0)
{
f->prior = fp->prior;
fp->prior->next = f;
f->data.address = fp->data.address;
f->data.size += fp->data.size;
delete fp;
}
if (fn != nullptr && fn->data.flag == 0)
{
f->next = fn->next;
fn->next->prior = f;
f->data.size += fn->data.size;
delete fn;
}
}
int main()
{
init();
//最佳适应算法
printf("最佳适应算法:\n");
init();
best_fit(1,15);
best_fit(2,30);
recycle(1);
best_fit(3,8);
best_fit(4,6);
recycle(2);
show();
DuNode *p = m_head;
while(p != NULL)
{
DuNode *temp = p;
p = p->next;
delete(temp);
temp = NULL;
}
return 0;
}