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
c语言用单链表实现lru算法,利用单链表实现LRU算法相关推荐
- python图像分割算法_Opencv(二)—图像分割之分水岭算法!
做图像处理时,我们可能会遇到一个问题:我们只需要图片的一部分区域,如何把图片中的某部分区域提取出来 或者 图像想要的区域用某种颜色(与其它区域颜色不一致)标记起来 ,以上描述的问题在像处理领域称为 图 ...
- 「从零入门推荐系统」08:召回算法之5类基础召回算法
作者 | gongyouliu 编辑 | gongyouliu 我们在上一篇文章中介绍了规则策略召回算法,这类方法非常简单,只需要利用一些业务经验和基础的统计计算就可以实现了.本节我们来讲解一些基础的 ...
- 数据结构与算法:单链表(利用万能指针实现对任意类型数据进行操作)
前言 C语言的指针真的很强大,万能指针更强大,可以指向任意类型的数据.在上篇博客 数据结构与算法:单链表(超详细实现)中用C语言实现了单链表的相关算法,不过却有局限性 只能针对某一种数据类型还是不够强 ...
- C语言丨线性表(二):线性链表(单链表)
线性表是由数据类型相同的个数据元素组成的有限序列,通常记为: 其中n为表长,n=0时称为空表:下标i表示数据元素的位序. 线性表的特点是组成它的数据元素之间是一种线性关系,即数据元素"一个接 ...
- 十、分享一道LeetCode较为简单的单链表题,但是却能激发起练习算法的极大的兴趣
实现一种算法,找出单向链表中倒数第 k 个节点.返回该节点的值 1.今天在LeetCode练习一道单链表的题(题目如上),虽然不难,但是却极大的触动了我学习算法的兴趣.因为当你看到大神的解法时,你真的 ...
- 数据结构与算法:单链表(超详细实现)
实现算法预览 这次博主写的单链表主要实现了以下算法.所有功能可进行循环运行测试.欢迎各位指正. LinkList.h #pragma once #ifndef __LINKLIST_H__ #defi ...
- 【C语言】数据结构C语言版 实验2 不带头结点的单链表
运行环境:Dev-C++ vs2013可能不能运行 首先新建一个头文件slnklist.h #include <stdio.h> #include <stdlib.h> /** ...
- 单链表的合并算法_图解算法:单链表两两反转 | 眼睛会了手就会系列
一. 序 链表作为一种基本的数据结构,本身理解起来,很简单.它通过指针或者叫引用,将一组零散的内存空间(结点),串联起来组成一个数据存储结构. 链表根据其指针的指向和丰富程度,可以分为单链表.双向链表 ...
- 数据结构与算法之单链表
数据结构与算法之单链表 //链表的实现/*实现单链表的 构建.数据添加.数据删除(返回元素所在位置).数据查找(返回元素所在的位置)的算法设计:*/ //链表的实现/*实现单链表的 构建.数据添加.数 ...
最新文章
- python爬虫数据分析可以做什么-python爬虫爬取的数据可以做什么
- async/await的实质理解
- ajax mysql项目 react_React16时代,该用什么姿势写 React ?
- java indexof 忽略大小写_javascript的indexOf如何才能忽略大小写
- could not find curses mysql_​-- Configuring incomplete-- Could NOT find Curses
- Microsoft REST API指南
- 前端学习(3274):js中this的使用三
- springmvc通过@Value注解读取Properties配置文件的值,junit测试可以取到值,但是在业务中无法读取
- 给一个执行在windows 7和NAT下的VMWARE虚拟机分配固定IP
- java行数可变的表格,怎么在表格中自动增加行,并对输入的数据作判断
- 中文信息处理(四)—— 神经网络基础
- yeoman、bower、grunt 开发收集
- unity 手机上获取手指触摸位置_我们到底触摸到了什么?揭秘智能设备触摸屏原理...
- shiro身份认证(HelloWorld)
- hibernate使用Query进行查询
- 关于送货单的格式要求?
- Imperva WAF 添加黑名单
- 【java删除指定文件夹或者文件】
- 《薛兆丰的经济学课》课程总结3--生命有限
- c语言如何实现高内聚低耦合_如何实现高内聚低耦合?高内聚低耦合的现实例子...