头歌实验六 动态分区算法
第1关:首次适应算法
#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 = m_head;
while(p != m_last) {
DuLinkList n = p->next;
if(!n->data.flag && n->data.size >= m_size) {
DuLinkList t = new DuNode();
t->data.ID = id;
t->data.size = m_size;
t->data.address = n->data.address;
t->data.flag = 1;
n->data.address += m_size;
n->data.size -= m_size;
t->prior = p;
t->next = n;
p->next = t;
n->prior = t;
}
p = n;
}
}
void recycle(int id)//回收内存,id为释放内存的作业号
{
//请补充回收作业内存的代码
DuLinkList p = m_head;
while(p != m_last) {
DuLinkList n = p->next;
if(n->data.ID == id) {
p->next = n->next;
n->next->prior = p;
n->next->data.address -= n->data.size;
m_last->data.size += n->data.size;
delete n;
n = n->next;
}
if(n != m_last)
n->next->data.address = n->data.address + n->data.size;
p = n;
}
}
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;
}
第2关:最佳适应算法
#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)//最佳适应算法,其中需要查找最佳的存放位置
{
//请补充使用最佳适应算法给作业分配内存的函数代码
DuLinkList p = m_head;
DuLinkList t = nullptr;
while(p != m_last) {
DuLinkList n = p->next;
if(!n->data.flag && n->data.size >= m_size) {
if(t == nullptr || t->data.size > n->data.size){
t = n;
}
}
p = n;
}
if(t == nullptr)
return false;
if(t->data.size == m_size) {
t->data.flag = 1;
t->data.ID = id;
}
else {
DuLinkList t2 = new DuNode();
t2->next = t;
t2->prior = t->prior;
t->prior->next = t2;
t->prior = t2;
t2->data.ID = id;
t2->data.flag = 1;
t2->data.address = t->data.address;
t->data.address += m_size;
t2->data.size = m_size;
t->data.size -= m_size;
}
return true;
}
void recycle(int id)//回收内存,id为释放内存的作业号
{
//请补充回收作业内存的函数代码
DuLinkList p = m_head;
DuLinkList n = nullptr;
while(p != m_last) {
n = p->next;
if(n->data.ID == id) {
break;
}
p = n;
}
if(n == nullptr || n == m_last)
return;
n->data.flag = 0;
n->data.ID = 0;
DuLinkList np = n->prior;
DuLinkList nn = n->next;
if(np != m_head && np->data.flag == 0) {
np->prior->next = n;
n->prior = np->prior->next;
n->data.address = np->data.address;
n->data.size += np->data.size;
delete np;
}
if((nn != nullptr || nn != m_last) && nn->data.flag == 0) {
nn->next->prior = n;
n->next = nn->next->prior;
n->data.size += nn->data.size;
delete nn;
}
}
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;
}