LRU(Least Recently Used)置换算法

利用一个特殊的站保存当前使用的各个页面的页面号,每当进程访问某页面时,便将该页面的页面号从占中移出,将它压入栈顶。因此,栈顶始终是最新被访问页面的编号,而栈底则是最近最久未使用页面的页面号。

 1 package test1;
 2 
 3 import java.util.Stack;
 4 
 5 public class LeastRecentPage
 6 {
 7   private static Stack<Integer> cache;
 8 
 9     // METHOD SIGNATURE BEGINS, THIS METHOD IS REQUIRED
10     public static int lruCountMiss(int max_cache_size, int[] pages) {
11         
12         int missCount = 0;
13         
14         if (cache == null) {
15             cache = new Stack<Integer>();
16         }
17 
18         int length = pages.length;
19         for (int j = 0;j < length; j++){
20             if(cache.contains(pages[j])){
21                 cache.removeElement(pages[j]);
22                 cache.push(pages[j]);
23             }else {
24                 if(cache.size() == max_cache_size){
25                     cache.remove(0);
26                     cache.push(pages[j]);
27                     missCount++;
28                 }else if(cache.size() < max_cache_size){
29                     cache.push(pages[j]);
30                     missCount++;
31                 }
32             }
33         }
34     
35         return missCount;
36     }
37   // METHOD SIGNATURE ENDS
38     
39     public static void main(String[] args) {
40         int count = lruCountMiss(2,new int[]{2,3,1,3,2,1,4,3,2});
41         System.out.println(count);
42     }
43 }


java 在util下提供了Stack类,其中包含主要的方法:(摘自java api)

1. push

public E push(E item)
  Pushes an item onto the top of this stack. This has exactly the same effect as:
 addElement(item)
  Parameters:
  item  - the item to be pushed onto this stack.
  Returns:
  the  item argument.
  See Also:
  Vector.addElement(E)

2. pop

public E pop()
  Removes the object at the top of this stack and returns that object as the value of this function.
  Returns:
The object at the top of this stack (the last item of the  Vector object).
  Throws:
EmptyStackException  - if this stack is empty.

3. peek

public E peek()
  Looks at the object at the top of this stack without removing it from the stack.
  Returns:
the object at the top of this stack (the last item of the  Vector object).
  Throws:
EmptyStackException  - if this stack is empty.

4. empty

public boolean empty()
  Tests if this stack is empty.
  Returns:
true  if and only if this stack contains no items; false otherwise.

 5. search

public int search(Object o)
  Returns the 1-based position where an object is on this stack. If the object  o occurs as an item in this stack, this method returns the distance from the top of the stack of the     occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack.
  Parameters:
o  - the desired object.
  Returns:
the 1-based position from the top of the stack where the object is located; the return value  -1 indicates that the object is not on the stack.

转载于:https://www.cnblogs.com/TGmoving/p/4842884.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值