最近最久未使用调度算法

xiaoxiao2021-02-28  97

/*最近最久未使用调度算法*/ #include<stdio.h> #include<malloc.h> #define N 100 int Butter[N]={-1}; void Change(int &a,int &b){ int temp; temp=a; a=b; b=temp; } void Moving(int * piror){     while(*piror!=-1){ Change(*piror,*(piror-1)); piror++; } } bool CheckFull(int a[],int n){ int num=0; while(a[num++]!=-1){ } if(num==n+1){ return true; } return false; } int * CreateStack(int n){ int  * body; body=(int *)malloc(sizeof(int)*n); for(int i=0;i<n+1;i++){ *(body+i)=-1; } return body; } int CheckFind(int a[],int n,int data){ for(int i=0;i<n;i++){ if(a[i]==data){ return i; } } return -1; } void Print(int a[],int n){ printf("当前内存块中的数据:"); for(int i=0;i<n;i++){ printf("%d ",a[i]); } printf("\n"); } int main(){ int *fir; int num,n,l=0,k=0; printf("物理块的大小:"); scanf("%d",&n); printf("数据的个数:"); scanf("%d",&num); fir=CreateStack(n); for(int i=0;i<num;i++){ printf("数据%d:",i+1); scanf("%d",Butter+i); } for(int j=0;j<num;j++){ if(CheckFull(fir,n)==false){ k=CheckFind(fir,n,Butter[j]); if(k==-1){ *(fir+l)=Butter[j]; l++; } else{ Change(*(fir+l),Butter[j]); Moving(fir+k+1); *(fir+l)=-1; } } else{ k=CheckFind(fir,n,Butter[j]); if(k==-1){ Moving(fir+1); Change(*(fir+n-1),Butter[j]); } else{ Moving(fir+k+1); Change(*(fir+n-1),Butter[j]); } } Print(fir,n); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-80005.html

最新回复(0)