c语言用单链表实现lru算法,利用单链表实现LRU算法

利用单链表实现LRU算法

首先,我们先看下对于Node的定义:

package datastructure.linklist.node;

import lombok.Data;

/**

* 单链表节点

* @author BiggerShen

* @date 2020/3/25

*/

@Data

public class SingletonNode {

private T data;

private SingletonNode next;

}

接下来,我们通过

随机生成一堆数字,代表要读取的页码

实现LRU算法

package datastructure.linklist.operate;

import datastructure.linklist.node.SingletonNode;

import java.util.ArrayList;

import java.util.List;

import java.util.Objects;

import java.util.Random;

/**

* using singleton link list to achive a LRU algorithm

*

* @author BiggerShen

* @date 2020/3/25

*/

public class LRUBySingletonLinkList {

/**

* Randomly generate a list connection with listSize,

* and minNum is the mininum value of the list and must be greater than zero,

* and maxNum is the maxnum value of the list and must be greater than zero

*

* @param listSize the size of the list connection

* @param minNum the mininum value of the list connection

* @param maxNum the maxnum value of the list connection

* @return

*/

public static List randomIntegerList(Integer listSize, Integer minNum, Integer maxNum) {

if (listSize <= 0) {

throw new RuntimeException("the size of the list connection must be greater than zero");

}

if (minNum < 0) {

throw new RuntimeException("the mininum of the list connection must be greater than zero");

}

if (maxNum < 0) {

throw new RuntimeException("the maxnum of the list connection must be greater than zero");

}

if (maxNum < minNum) {

throw new RuntimeException("the maxNum must be greater than minNum");

}

int sub = maxNum - minNum;

List result = new ArrayList<>(realListCapacity(listSize));

for (int i = 0; i < listSize; i++) {

result.add(new Random().nextInt(sub + 1) + minNum);

}

return result;

}

/**

* Returns a power of two size for the given target capacity.

*

* @param originCapacity

* @return

*/

public static Integer realListCapacity(Integer originCapacity) {

int n = originCapacity - 1;

n |= n >>> 1;

n |= n >>> 2;

n |= n >>> 4;

n |= n >>> 8;

n |= n >>> 16;

return (n < 0) ? 1 : n + 1;

}

/**

* achive the LRU algorithm

*

* @param readList random List

* @param LRUCacheNum LRU CacheNum

* @return

*/

public static SingletonNode getLinkListByLRU(List readList, Integer LRUCacheNum) {

SingletonNode head = new SingletonNode<>();

readList.forEach(i -> {

if (checkValueIsContainedByLinkList(head, i)) {

swapTheValueToHead(head, i);

} else {

if (getLinkListSize(head) >= LRUCacheNum) {

deleteLastNode(head);

swapTheValueToHead(head, i);

} else {

swapTheValueToHead(head, i);

}

}

showList(head);

});

return head;

}

/**

* get the size of the link connection

*

* @param head

* @return

*/

private static Integer getLinkListSize(SingletonNode head) {

SingletonNode tmp = head;

int size = 0;

while (!Objects.isNull(tmp)) {

size++;

tmp = tmp.getNext();

}

return size;

}

/**

* Determines whether this value exists in the current collection

*

* @return

*/

private static Boolean checkValueIsContainedByLinkList(SingletonNode head, Integer value) {

SingletonNode tmp = head;

while (!Objects.isNull(tmp)) {

if (Objects.equals(tmp.getData(), value)) {

return true;

}

tmp = tmp.getNext();

}

return false;

}

/**

* delete the last one of the list value

*

* @param head

*/

private static void deleteLastNode(SingletonNode head) {

SingletonNode tmp = head;

SingletonNode tmpPrev = null;

while (!Objects.isNull(tmp.getNext())) {

tmpPrev = tmp;

tmp = tmp.getNext();

}

tmpPrev.setNext(null);

}

/**

* change the target value to the head

*

* @param head

* @param value

*/

private static void swapTheValueToHead(SingletonNode head, Integer value) {

SingletonNode tmpValue = new SingletonNode<>();

tmpValue.setData(value);

SingletonNode tmp = head;

tmpValue.setNext(tmp.getNext());

tmp.setNext(tmpValue);

tmp = tmpValue.getNext();

SingletonNode tmpPrev = tmpValue;

while (!Objects.isNull(tmp)) {

if (Objects.equals(tmp.getData(), value)) {

tmpPrev.setNext(tmp.getNext());

return;

}

tmpPrev = tmp;

tmp = tmp.getNext();

}

}

/**

* print the link list

*

* @param head

*/

private static void showList(SingletonNode head) {

SingletonNode tmp = head;

while (!Objects.isNull(tmp)) {

if (Objects.isNull(tmp.getData())) {

tmp = tmp.getNext();

continue;

}

System.out.print(tmp.getData() + "\t");

tmp = tmp.getNext();

}

System.out.println();

}

public static void main(String[] args) {

List randomReadList = randomIntegerList(20, 1, 10);

System.out.println(randomReadList);

SingletonNode head = getLinkListByLRU(randomReadList, 4);

}

}

接下来,我们看下运行结果:

[1, 2, 4, 4, 6, 2, 4, 4, 9, 1, 10, 6, 4, 1, 4, 1, 3, 5, 6, 10]

1

2 1

4 2 1

4 2 1

6 4 2

2 6 4

4 2 6

4 2 6

9 4 2

1 9 4

10 1 9

6 10 1

4 6 10

1 4 6

4 1 6

1 4 6

3 1 4

5 3 1

6 5 3

10 6 5

转载请注明来源,欢迎指出任何有错误或不够清晰的表达。可以邮件至 gouqiangshen@126.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值