请珍惜小编劳动成果,该文章为小编原创,转载请注明出处。
LRU算法,即Last Recently Used ---选择最后一次訪问时间距离当前时间最长的一页并淘汰之——即淘汰最长时间没有使用的页
依照最多5块的内存分配情况,实现LRU算法代码例如以下:
public class LRU {
private int theArray[];
private int back; //定义队尾
private int currentSize; //队列中存放元素个数
private int maxSize=5; //队列中能存放元素的个数
public LRU(){
theArray=new int[maxSize];
back=0;
currentSize=0;
}
public void queue(int a[]){
for(int i=0;i
enQueue(a[i]);
}
}
public void enQueue(int x){ //入队
beUsed(x); //先推断是否已存在该页号,若存在,删除
if(currentSize
theArray[back]=x;
back++;
currentSize++;
}else if(currentSize==maxSize){ //满了
for(int i=0;i
theArray[i]=theArray[i+1];