栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

模拟实现首次/最佳/最坏适应算法的内存块分配和回收

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模拟实现首次/最佳/最坏适应算法的内存块分配和回收

编写C语言程序,模拟实现首次/最佳/最坏适应算法的内存块分配和回收,要求每次分配和回收后显示出空闲分区和已分配分区的情况。

假设初始状态下,可用的内存空间为640KB。
#include
#define ll long long
using namespace std;

const ll maxn=1e18;
const ll minn=-1e18;
const ll mod=1000000007;
const ll MAX=100005;
bool cmp(ll a,ll b){return a>b;}


int n;  //初始空间
string algin;
int pos=1;  //记录当前内存块的个数;


struct node{
    int left,right;
    int vis;    //0表示未使用内存;1表示已经使用了内存空间;
    int work;
    bool operator < (const node &t) const
    {
        return left
    string FIR="FIR";
    string BST="BST";
    string WST="WST";
    sort(node+1,node+pos+1);
    if(algin==FIR)   //首次
    {
        for(int i=1 ; i<=pos ; i++)
        {
            if(node[i].right-node[i].left>=nulen&&node[i].vis==0)
            {
                pos++;
                node[pos].left=node[i].left;
                node[pos].right=node[pos].left+nulen;
                node[pos].vis=1;
                node[pos].work=nu;
                node[i].left=node[pos].right;
                cout<
        vector temp;
        for(int i=1 ; i<=pos ; i++)
        {
            if(node[i].right-node[i].left==nulen&&node[i].vis==0)   //刚好的时候一定是最优;
            {
                node[i].vis=1;
                node[i].work=nu;
                cout<
                temp.push_back(i);
            }
        }
        int len=temp.size();
        sort(temp.begin(),temp.end());    //升序;
        for(int i=0 ; i
            if(node[temp[i]].right-node[temp[i]].left>nulen)
            {
                pos++;
                node[pos].left=node[temp[i]].left;
                node[pos].right=node[pos].left+nulen;
                node[pos].vis=1;
                node[pos].work=nu;
                node[temp[i]].left=node[pos].right;
                cout<
        vector temp;
        for(int i=1 ; i<=pos ; i++)
        {
            if(node[i].vis==0)
            {
                temp.push_back(i);
            }
        }
        int len=temp.size();
        sort(temp.begin(),temp.end(),cmp);    //降序;
        for(int i=0 ; i
            if(node[temp[i]].right-node[temp[i]].left>=nulen)
            {
                if(node[temp[i]].right-node[temp[i]].left==nulen)   //相等
                {
                    node[temp[i]].vis=1;
                    node[temp[i]].work=nu;
                    cout<
                pos++;
                node[pos].left=node[temp[i]].left;
                node[pos].right=node[pos].left+nulen;
                node[pos].vis=1;
                node[pos].work=nu;
                node[temp[i]].left=node[pos].right;
                cout<
    sort(node+1,node+pos+1);
    for(int i=1 ; i<=pos ; i++)
    {
        if(node[i].work==nu)
        {
            node[i].vis=0;
            node[i].work=0;
            int temp=0;
            for(int j=i-1 ; j>=1 ; j--)      //从后往前找
            {
                if(node[j].vis!=0)
                    break;
                else
                {
                    node[i].left=node[j].left;
                    node[j].left=n;
                    node[j].right=n;
                    temp++;
                }
            }
            for(int j=i+1 ; j<=pos ; j++)   //从前往后找
            {
                if(node[j].vis!=0)
                    break;
                else
                {
                    node[i].right=node[j].right;
                    node[j].left=n+1;
                    node[j].right=n+1;
                    temp++;
                }
            }
            sort(node,node+pos);
            pos-=temp;
        }
    }
}







void print()
{
    sort(node+1,node+pos+1);
    cout<
        cout<<"|<-";
        for(int j=0 ; j<(node[i].right-node[i].left)/20-5; j++)
            cout<<" ";
        if(node[i].vis==0)
            cout<<"None";
        else
            cout<<"work"<|"<";
    }
    //底线
    for(int i=0 ; i<(node[pos].right-node[1].left)/10 ; i++)
        cout<<"-";
    cout<
        for(int j=0 ; j<(node[i].right-node[i].left)/10-3 ; j++)
            cout<<" ";
        cout<
    node[0].left=0;
    node[0].right=0;
    node[0].vis=0;
    node[0].work=0;
    node[pos].left=node[pos-1].right;
    node[pos].right=n;
    node[pos].vis=0;
    node[pos].work=0;
    print();
}
void init()
{
    cout<<"请输入初始系统空间大小:"<>n;
    fi(n);
    int t;
    while(1)
    {
        cout<<"请输入操作:1-插入 2-删除 0-退出"<>t;
        if(t==0)
            break;
        else if(t==1)
        {
            cout<<"请输入插入所适应算法:FIR-首次 BST-最佳 WST-最坏:"<>algin;
            cout<<"请输入需要分配的作业号和大小"<>nu>>nulen;
            opin(nu,nulen);
            //printtest();
            print();
        }
        else if(t==2)
        {
            cout<<"请输入需要回收的作业号"<>nu;
            opde(nu);
            print();
            //printtest();
        }
    }

}
int main()
{
    init();
    return 0;
}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号