cin>>nm;
while(btemp!
=NULL)//确定要回收的内存空间
{
if(btemp->name==nm)
break;
btemp=btemp->next;
}
while(ftemp!
=NULL)//回收选定的内存空间
{
if(btemp->endAddresshead->startAddress)//要回收的内存在空闲链表之前
{
if(ftemp->startAddress==btemp->endAddress+1)
{
ftemp->startAddress=btemp->startAddress;
ftemp->size+=btemp->size;
ftemp->prior=NULL;
}
else
{
busylist->head=btemp->next;
if(busylist->head)
busylist->head->prior=NULL;
btemp->next=freelist->head;
btemp->prior=NULL;
freelist->head->prior=btemp;
freelist->head=btemp;
freelist->head->prior=NULL;
finish=true;
}
}
elseif(btemp->startAddress>ftemp->endAddress&&btemp->endAddressnext->startAddress)
{//要回收的内存在空闲链表之中
if(btemp->startAddress==ftemp->endAddress+1&&btemp->endAddress==ftemp->next->startAddress-1)
{//上下皆邻
ftemp->size=ftemp->size+btemp->size+ftemp->next->size;
ftemp->endAddress=ftemp->startAddress+ftemp->size-1;
if(ftemp->next->next)
ftemp->next->next->prior=ftemp;
ftemp->next=ftemp->next->next;
}
elseif(btemp->startAddress==ftemp->endAddress+1)
{//上邻
ftemp->size+=btemp->size;
ftemp->endAddress=ftemp->startAddress+ftemp->size-1;
}
elseif(btemp->endAddress==ftemp->next->startAddress-1)
{//下邻
ftemp->next->startAddress=btemp->startAddress;
ftemp->size=ftemp->next->endAddress-ftemp->next->startAddress+1;
}
else{//上下都不相邻
btemp->next=ftemp->next;
ftemp->next->prior=btemp;
ftemp->next=btemp;
btemp->prior=ftemp;
}
}
ftemp=ftemp->next;
}
if(!
btemp->prior&&finish==false)
{
busylist->head=btemp->next;
if(busylist->head)
busylist->head->prior=NULL;
}
elseif(finish==false){
if(btemp->next)
btemp->next->prior=btemp->prior;
btemp->prior->next=btemp->next;
}
}
运行结果:
initial...
freelistinformation--startadd--endadd--size--
---0----99----100
busylistinformation--name--startadd--endadd--size--
selectallocetion
(1)orreclaim
(2):
1
plseaeinputanewblockinformation:
--name--startAdd--size--
a2025
ithasbeenallocated!
freelistinformation--startadd--endadd--size--
---0----19----20
---45----99----55
busylistinformation--name--startadd--endadd--size--
---a---20---44---25
1
plseaeinputanewblockinformation:
--name--startAdd--size--
b4820
ithasbeenallocated!
freelistinformation--startadd--endadd--size--
---0----19----20
---45----47----3
---68----99----32
busylistinformation--name--startadd--endadd--size--
---a---20---44---25
---b---48---67---20
1
plseaeinputanewblockinformation:
--name--startAdd--size--
c7010
ithasbeenallocated!
freelistinformation--startadd--endadd--size--
---0----19----20
---45----47----3
---68----69----2
---80----99----20
busylistinformation--name--startadd--endadd--size--
---a---20---44---25
---b---48---67---20
---c---70---79---10
2
pleaseinputthenameofreclaimedblock:
b
freelistinformation--startadd--endadd--size--
---0----19----20
---45----69----25
---80----99----20
busylistinformation--name--startadd--endadd--size--
---a---20---44---25
---c---70---79---10