public classLRUPageFrame {private static classNode {
Node prev;
Node next;intpageNum;
Node() {
}
}private intcapacity;private intcurrentSize;private Node first;//链表头
private Node last;//链表尾
public LRUPageFrame(intcapacity) {this.currentSize = 0;this.capacity =capacity;
}/*** 获取缓存中对象
*
*@paramkey
*@return
*/
public void access(intpageNum) {
Node node=find(pageNum);//在该队列中存在, 则提到队列头
if (node != null) {
moveExistingNodeToHead(node);
}else{
node= newNode();
node.pageNum=pageNum;//缓存容器是否已经超过大小.
if (currentSize >=capacity) {
removeLast();
}
addNewNodetoHead(node);
}
}private voidaddNewNodetoHead(Node node) {if(isEmpty()){
node.prev= null;
node.next= null;
first=node;
last=node;
}else{
node.prev= null;
node.next=first;
first.prev=node;
first=node;
}this.currentSize ++;
}private Node find(intdata){
Node node=first;while(node != null){if(node.pageNum ==data){returnnode;
}
node=node.next;
}return null;
}/*** 删除链表尾部节点 表示 删除最少使用的缓存对象*/
private voidremoveLast() {
Node prev=last.prev;
prev.next= null;
last.prev= null;
last=prev;this.currentSize --;
}/*** 移动到链表头,表示这个节点是最新使用过的
*
*@paramnode*/
private voidmoveExistingNodeToHead(Node node) {if (node ==first) {return;
}else if(node ==last){//当前节点是链表尾, 需要放到链表头
Node prevNode =node.prev;
prevNode.next= null;
last.prev= null;
last=prevNode;
}else{//node 在链表的中间, 把node 的前后节点连接起来
Node prevNode =node.prev;
prevNode.next=node.next;
Node nextNode=node.next;
nextNode.prev=prevNode;
}
node.prev= null;
node.next=first;
first.prev=node;
first=node;
}private booleanisEmpty(){return (first == null) && (last == null);
}
}