《首次适应算法,最佳适应算法,最坏适应算法源代码》由会员分享,可在线阅读,更多相关《首次适应算法,最佳适应算法,最坏适应算法源代码(11页珍藏版)》请在金锄头文库上搜索。
1、首次适应算法首次适应算法, ,最佳适应算法最佳适应算法, ,最坏适应算法源代码最坏适应算法源代码#include#include#define Free 0 /空闲状态#define Busy 1 /已用状态#define OK 1 /完成#define ERROR 0 /出错#define MAX_length 640 /最大内存空间为 640KBtypedef int Status;int flag;typedef struct freearea/定义一个空闲区说明表结构long size; /分区大小long address; /分区地址int state; /状态ElemType;/
2、线性表的双向链表存储结构typedef struct DuLNodeElemType data; struct DuLNode *prior; /前趋指针struct DuLNode *next; /后继指针DuLNode,*DuLinkList; DuLinkList block_first; /头结点DuLinkList block_last; /尾结点Status alloc(int);/内存分配Status free(int); /内存回收Status First_fit(int);/首次适应算法Status Best_fit(int); /最佳适应算法Status Worst_fit
3、(int); /最差适应算法void show();/查看分配Status Initblock();/开创空间表 Status Initblock()/开创带头结点的内存空间链表block_first=(DuLinkList)malloc(sizeof(DuLNode);block_last=(DuLinkList)malloc(sizeof(DuLNode);block_first-prior=NULL;block_first-next=block_last;block_last-prior=block_first;block_last-next=NULL;block_last-data.a
4、ddress=0;block_last-data.size=MAX_length;block_last-data.state=Free;return OK;/分配主存Status alloc(int ch)int request = 0;coutrequest;if(requestdata.size=request;temp-data.state=Busy;DuLNode *p=block_first-next;while(p)if(p-data.state=Free return OK;break;if(p-data.state=Free temp-next=p;temp-data.addr
5、ess=p-data.address;p-prior-next=temp; p-prior=temp;p-data.address=temp-data.address+temp-data.size;p-data.size-=request;return OK;break;p=p-next;return ERROR;/最佳适应算法Status Best_fit(int request)int ch; /记录最小剩余空间DuLinkList temp=(DuLinkList)malloc(sizeof(DuLNode); temp-data.size=request;temp-data.state
6、=Busy;DuLNode *p=block_first-next;DuLNode *q=NULL; /记录最佳插入位置while(p) /初始化最小空间和最佳位置if(p-data.state=Free ch=p-data.size-request;else if(q-data.size p-data.size)q=p;ch=p-data.size-request;p=p-next;if(q=NULL) return ERROR;/没有找到空闲块else if(q-data.size=request)q-data.state=Busy;return OK;elsetemp-prior=q-p
7、rior;temp-next=q;temp-data.address=q-data.address;q-prior-next=temp;q-prior=temp;q-data.address+=request;q-data.size=ch;return OK;return OK;/最差适应算法Status Worst_fit(int request)int ch; /记录最大剩余空间DuLinkList temp=(DuLinkList)malloc(sizeof(DuLNode); temp-data.size=request;temp-data.state=Busy;DuLNode *p=
8、block_first-next;DuLNode *q=NULL; /记录最佳插入位置while(p) /初始化最大空间和最佳位置if(p-data.state=Free ch=p-data.size-request;else if(q-data.size data.size)q=p;ch=p-data.size-request;p=p-next;if(q=NULL) return ERROR;/没有找到空闲块else if(q-data.size=request)q-data.state=Busy;return OK;elsetemp-prior=q-prior;temp-next=q;te
9、mp-data.address=q-data.address;q-prior-next=temp;q-prior=temp;q-data.address+=request;q-data.size=ch;return OK;return OK;/主存回收Status free(int flag)DuLNode *p=block_first;for(int i= 0; i next;elsereturn ERROR;p-data.state=Free;if(p-prior!=block_first p-prior-next=p-next;p-next-prior=p-prior;p=p-prior
10、;if(p-next!=block_last p-next-next-prior=p;p-next=p-next-next; if(p-next=block_last p-next=NULL; return OK;/显示主存分配情况void show()int flag = 0;coutnext;coutdata.addressdata.sizedata.state=Free) coutnext;coutch;while(ch3)coutch;Initblock(); /开创空间表int choice; /操作选择标记while(1)show();coutchoice;if(choice=1) alloc(ch); / 分配内存else if(choice=2) / 内存回收int flag;coutflag;free(flag);else if(choice=0) break; /退出else /输入操作有误cout“输入有误,请重试!“endl;continue;