}
void sort() //对空闲链表进行排序 { int max;
node *p,*q,*r,*s; node a; p=L.after; while(p!=NULL) //让指针q指向链表的最后一个结点 { q=p; p=p->after; } if(L.after->after==NULL) return; else { while(p!=q) {
s=r=p=L.after; max=r->size; while(s!=q->after) { if(s->size>max) { max=s->size; r=s; s=s->after; } else s=s->after; } a.size=q->size; a.address=q->address; q->size=r->size; q->address=r->address; r->size=a.size; r->address=a.address; if(q->before->before==&L) return;
else q=q->before; } } }
void Print() { node *p=L.after; usenode *q=U.next; int i=1; printf(\空闲区域列表:\\n\ printf(\ address size\\n\ while(p!=NULL) { printf(\ printf(\ printf(\ p=p->after; i++; } if(q==NULL) return; else { printf(\已分配区域列表:\\n\ printf(\ address size\\n\ while(q!=NULL) { printf(\ printf(\ printf(\ q=q->next; } } }
void firstfit() //首次适应算法 {
int a,b,i; Init(); Print(); while(1) {printf(\、申请空间\\n\ printf(\、释放空间\\n\ printf(\、退出首次适应算法\\n\ printf(\请输入你的选择:\ scanf(\ switch(i) { case 1: {
printf(\请输入申请空间的作业号:\ scanf(\ printf(\请输入申请空间的大小:\ scanf(\ alloc(a,b); Print(); break; } case 2: { printf(\请输入释放空间的作业号:\ scanf(\ printf(\请输入释放空间的大小:\ scanf(\ recovery(a,b); Print(); break; } case 3:printf(\ } } }
void bestfit() { int a,b,i; Init(); Print(); while(1) {printf(\、申请空间\\n\
printf(\、释放空间\\n\ printf(\、退出最佳适应算法\\n\ printf(\请输入你的选择:\ scanf(\ switch(i) { case 1: {
printf(\请输入申请空间的作业号:\ scanf(\ printf(\请输入申请空间的大小:\ scanf(\ alloc(a,b); sort(); Print(); break; } case 2: { printf(\请输入释放空间的作业号:\ scanf(\ printf(\请输入释放空间的大小:\ scanf(\ recovery(a,b); sort(); Print(); break; } case 3:printf(\ } } }
void main() {
int i; while(1) { printf(\、首次适应算法\\n\ printf(\、最佳适应算法\\n\ printf(\、退出\\n\ printf(\请输入你的选择:\ scanf(\
switch(i) { case 1:firstfit();break; case 2:bestfit();break; case 3:return; } } }
运行结果
① 开始界面
② 首次适应算法
③ 最佳适应算法