📄 zhucunfenpeiyuhuishou.cpp
字号:
#include<stdio.h>
#include<malloc.h>
typedef struct freechain
{
int startadd;//起始地址
int state;//空闲区状态,0表示空闲,1表示被分配
int blocksize;//空间大小
struct freechain *front,*behind;//front指向下一个,behind指向上一个
}freechains;
freechains *head;
int n;
void output()
{
struct freechain *p;
p=head->front;
printf("起始地址 大小 状态\n");
do
{
printf("%d %-6d",p->startadd,p->blocksize);
if(p->state==0)
printf(" 未分配\n");
else
printf(" 已分配\n");
p=p->front;
}while(p!=NULL);
}
void alloc(struct freechain *m)//内存分配函数
{
int i,j=0,size,a,b;
m=head->front;
printf("输入需要分配内存的大小:");
scanf("%d",&size);
for(i=0;i<n;i++)
{
if(m->blocksize>=size)
{
j=1;
break;
}
else
m=m->front;
}
if(j==0)
printf("no comfortable place can be distribute!!!\n");
else if(m->blocksize>size)
{
a=m->blocksize;
b=m->startadd;
m->state=1;
m->blocksize=size;
m->startadd=m->startadd+a-size;
printf("分配后的内存状态为:\n");
output();
m->startadd=b;
m->blocksize=a-size;
m->state=0;
printf("分配后的空闲内存状态为:\n");
output();
}
else if(m->blocksize==size)
{
n--;
if(m->front==NULL&&m->behind==NULL)
{
m->state=1;
output();
printf("this chain is empty\n");
}
else if(m->behind==NULL)
{
m->state=1;
printf("分配后的内存状态为\n");
output();
head=m->front;
head->behind=NULL;
free(m);
printf("分配后的空闲内存状态为:\n");
output();
}
else if(m->front==NULL)
{
m->state=1;
printf("分配后的内存块链为:\n");
output();
m->behind->front=NULL;
free(m);
printf("分配后的空闲内存状态为:\n");
output();
}
else
{
m->state=1;
printf("分配后的内存块链为:\n");
output();
m->front->behind=m->behind;
m->behind->front=m->front;
free(m);
printf("分配后的空闲内存状态为:\n");
output();
}
n++;
}
}
void release(struct freechain *p)//回收函数
{
int i, j=0;
freechains *newnode,*freenode;
p=head->front;
newnode=(struct freechain *)malloc(sizeof(struct freechain));
printf("请输入需要回收内存块的首地址和长度!\n");
printf("起始地址:");
scanf("%d",&newnode->startadd);
printf("空间大小");
scanf("%d",&newnode->blocksize);
newnode->front=newnode->behind=NULL;
newnode->state=0;
for(i=0;i<n;i++)
{
if(p->startadd+p->blocksize==newnode->startadd||p->startadd
==newnode->startadd+newnode->blocksize)
{
j=1;
break;
}
else
p=p->front;
}
if(j==0)
{
p=head->front;
while(p->startadd<newnode->startadd&&p->front!=NULL)
{
p=p->front;
}
if(n==1&&newnode->startadd<p->startadd)
{
newnode->front=p;
p->behind=newnode;
head->front=newnode;
printf("回收后的空间内存块链:\n");
output();
}
else
{
if(p->startadd>newnode->startadd&&p->behind==NULL)
{
newnode->front=p;
p->behind=newnode;
head->front=newnode;
newnode->behind=NULL;
printf("回收后的空间内存块链:\n");
output();
}
else if(p->startadd<newnode->startadd&&p->front==NULL)
{
p->front=newnode;
newnode->front=NULL;
newnode->behind=p;
printf("回收后的空间内存块链:\n");
output();
}
else if(p->startadd>newnode->startadd&&p->behind)
{
p->behind->front=newnode;
newnode->front=p;
newnode->behind=p->behind;
printf("回收后的空间内存块链:\n");
output();
}
else if(p->startadd<newnode->startadd&&p->front)
{
p->front->behind=newnode;
newnode->front=p->front;
newnode->behind=p;
printf("回收后的空间内存块链:\n");
output();
}
}
}
else
{
if(p->front!=NULL&&p->startadd+p->blocksize+newnode->blocksize
==p->front->startadd)
{
if(p->front!=NULL)
{
freenode=p->front;
if(freenode->front!=NULL)
{
p->blocksize=p->blocksize+newnode->blocksize+p->front->blocksize;
p->front->front->behind=p;
p->front=(p->front->front);
printf("回收后的空间内存块链:\n");
output();
free(freenode);
}
else
{
freenode=p->front;
p->blocksize=p->blocksize+newnode->blocksize+p->front->blocksize;
p->front=NULL;
printf("回收后的空间内存块链:\n");
output();
free(freenode);
}
}
}
else if(p->startadd==newnode->startadd+newnode->blocksize)
{
p->startadd=newnode->startadd;
p->blocksize+=newnode->blocksize;
printf("回收后的空间内存块链:\n");
output();
}
else
{
p->blocksize+=newnode->blocksize;
printf("回收后的空间内存块链:\n");
output();
}
}
}
int main()
{
int i;
struct freechain *p,*q;
head=(struct freechain *)malloc(sizeof(struct freechain));
printf("请输入空闲的内存块个数:");
scanf("%d",&n);
if(n<1)
{
printf("error input!!!\n");
goto end;
}
else
{
head->front=(struct freechain *)malloc(sizeof(struct freechain));
printf("请输入空闲内存的信息!!!\n");
p=head->front;
p->state=0;p->behind=NULL;
printf("起始地址:");
scanf("%d",&p->startadd);
printf("空间大小:");
scanf("%d",&p->blocksize);
if(n==1)
{
p->front=NULL;
p->state=0;
}
else
{
for(i=1;i<n;i++)
{
p->front=(struct freechain *)malloc(sizeof(struct freechain));
q=p->front;
q->state=0;
q->behind=p;
printf("起始地址:");
scanf("%d",&q->startadd);
printf("空间大小:");
scanf("%d",&q->blocksize);
p=p->front;
}
q->front=NULL;
}
}
alloc(p);
release(p);
end:return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -