⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zhucundefenpeihehuishou.cpp

📁 操作系统课程设计之内存分配回收
💻 CPP
字号:
#include"iostream.h"                                                                                                                                           
#include"stdlib.h"
#include"stdio.h" 
#include<iomanip.h>
const int Max=128;
typedef struct m
{
	int start;
    int length;
    int state;
    struct m *next;
}M;
M *init(M *head)
{
	M *work;
    M *p;
	work=(M *)new(M);
    work->start=0;work->length=5;work->next=NULL;work->state=1;
    head=work;
    p=(M *)new(M);
    p->start=5;p->length=Max-4;p->next=NULL;p->state=-1;
    work->next=p; 
	return head;
}

void insert(M *head)
{
    int length,flag=0,temp;
    M *r=(M *)new(M);
    M *ntemp=(M *)new(M);
	cout<<"输入作业的长度:";  cin>>length;
    if(length>Max)
    {
		cout<<"系统空间不足,不能装入作业!!退出..."<<endl;
        exit(0);
    }
    r=head;
    while(r!=NULL)
    {
        if(r->state==1)
        {  r=r->next;continue;  }
        else
        {
            if(r->length==length)
            {  r->state=1;flag=1; break; }
            if(r->length>length)
            {
                temp=r->length;
                r->length=length;
                r->state=1;
				ntemp->next=r->next;//ntemp最后尾节点
                ntemp->start=r->start+r->length;
                ntemp->length=temp-r->length;
                ntemp->state=-1;
                r->next=ntemp;
                flag=1;  break;
            }
            if(r->length<length)
            {
                r=r->next;
                continue;
            }
        }
    }    
    if(flag==0)
    {
		cout<<"不能装入作业!退出..."<<endl;
        exit(0);
    }
}
void dele (M *head)
{
    int start,y=0;M *s;
    M *r=(M *)new(M);
	M *temp=(M *)new(M);
	r=head;s=head;
	cout<<"要删除作业的起始地址:";
	cin>>start;
	while(s!=NULL)//是否有需要的开始地址
    {
        if(s->start==start) { y=1; break;}
		else  s=s->next;
    }    
    if(y==0) 
		cout<<"没有以地址"<<start<<"开始的作业..."<<endl;
	else
	{	
		while(r!=NULL)
		{
			if(r->start==start)
			{
				r->state=-1;
				break;
			}
			else r=r->next;
		}
		temp=head;
		while(temp->next!=NULL)
		{
			if(temp->state==-1&&temp->next->state==-1)
			{
				temp->length=temp->length+temp->next->length;
				temp->next=temp->next->next;
			}
			else temp=temp->next;
		}
	}
}
void dis(M *head)
{
    
    int state=-1;
    M *temp=(M *)new(M);
    temp=head;
	cout<<"\n----------空闲说明表---------"<<endl;
	cout<<"起始地址  内存长度  内存状态"<<endl;
	while(temp!=NULL)
    {
		cout<<setw(4)<<temp->start<<"     "<<setw(6)<<temp->length;
		if(temp->state==1)
		cout<<"       "<<"以用"<<endl;
		else  cout<<"      "<<"未分配"<<endl;
        temp=temp->next;
    }
	cout<<" "<<Max<<"          0      未分配"<<endl;
    cout<<"-----------------------------\n"<<endl;
    return ;
}
void main()
{
	M *head; int Choice,i=1;
    head=init(head);  dis(head);
	do
    {
		cout<<"*************"<<"第"<<i<<"次操作"<<"************"<<endl;
		cout<<"1.装入作业;2.删除作业;0.退出程序\n\n选择序号:";
		cin>>Choice;
        switch(Choice)
        {
            case 0: cout<<"\n退出..."<<endl;break;
            case 1:
				insert(head);
                dis(head);
                break;
            case 2:
                dele(head);
                dis(head);
                break;
            default:
                cout<<"输入正确的选择!"<<endl;
                break;
        }
		i++;
    }while(Choice!=0);
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -