c语言用单链表实现lru算法,手写单链表实现和LRU算法模拟
手写单链表,实现增删改查
package top.zcwfeng.java.arithmetic.lru;
//单链表
public class LinkedList {
Node list;
int size;//链表节点个数
public LinkedList() {
}
// 添加
public void put(T data) {
Node head = list;//保存一下
Node currentNode = new Node(data,list);
list = currentNode;
size ++;
}
public void put(int index, T data) {
checkPositonIndex(index);
Node cur = list;
Node head = list;// 保存当前的节点前面的节点
for (int i = 0; i < index; i++) {
head = cur;
cur = cur.next;
}
Node node = new Node(data,cur);
head.next = node;
size++;
}
/**
* 检查index 是否在链表的范围内
* @param index
*/
public void checkPositonIndex(int index) {
if(!(index >= 0 && index <= size)){
throw new IndexOutOfBoundsException("index:" + index + ",size:"+size);
}
}
// 修改
public void set(int index, T newData) {
checkPositonIndex(index);
Node head = list;// 保存当前的节点前面的节点
for (int i = 0; i < index; i++) {
head = head.next;
}
head.data = newData;
}
// 删除
/**
* 删除头部
*
* @return
*/
public T remove() {
if(list != null){
Node node = list;
list = list.next;
node.next = null;
size--;
return node.data;
}
return null;
}
public T remove(int index) {
checkPositonIndex(index);
Node head = list;
Node cur = list;
for (int i = 0; i < index; i++) {
head = cur;
cur = cur.next;
}
head.next = cur.next;
cur.next = null;
size--;
return cur.data;
}
public T removeLast() {
if(list != null){
Node node = list;
Node cur = list;
while (cur.next != null){
node = cur;
cur = cur.next;
}
node.next = null;
size --;
return cur.data;
}
return null;
}
// 查询
/**
* get head
*
* @return
*/
public T get() {
Node node = list;
if(node != null){
return list.data;
} else {
return null;
}
}
public T get(int index) {
checkPositonIndex(index);
Node node = list;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node.data;
}
@Override
public String toString() {
Node node = list;
for (int i = 0; i < size; i++) {
System.out.println(node.data + " ");
node = node.next;
}
System.out.println();
return super.toString();
}
// 节点信息
class Node {
T data;
Node next;
public Node(T data, Node next) {
this.data = data;
this.next = next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList<>();
for (int i = 0; i < 5; i++) {
list.put(i);
}
list.toString();
list.put(3,3);
list.toString();
list.put(8);
list.toString();
}
}
根据单链表操作,实现LRU算法
新数据插入到链表头部
当缓存命中(即缓存数据被访问),数据要移到表头
当链表满的时候,将链表尾部的数据丢弃
package top.zcwfeng.java.arithmetic.lru;
public class LruLinkedList extends LinkedList {
static final int DEFAULT_CAP = 5;
int memeory_size;//用于限定内存大小,也就是缓存大小
public LruLinkedList() {
this(DEFAULT_CAP);
}
public LruLinkedList(int memeory_size) {
this.memeory_size = memeory_size;
}
public void lruPut(T data) {
if(size >= memeory_size){
removeLast();
put(data);
}else{
put(data);
}
}
public T lruRemove(){
return removeLast();
}
public T lruGet(int index){
checkPositonIndex(index);
Node node = list;
Node pre = list;
for (int i = 0; i < index; i++) {
pre = node;
node = node.next;
}
T resultData = node.data;
// 将访问节点放到表头
pre.next = node.next;
Node head = list;
node.next = head;
list = node;
return resultData;
}
public static void main(String[] args) {
LruLinkedList lruLinkedList = new LruLinkedList<>();
for (int i = 0; i < 4; i++) {
lruLinkedList.lruPut(i);
}
lruLinkedList.toString();
System.out.println(lruLinkedList.lruGet(3));
lruLinkedList.toString();
lruLinkedList.lruPut(20);
lruLinkedList.toString();
lruLinkedList.lruPut(18);
lruLinkedList.toString();
}
}
给LinkeList 添加翻转单链表方法
public void reverse(){
//单链表为空或只有一个节点,直接返回原单链表
if (list == null || list.next == null){
return;
}
Node head = list;
Node next = list.next;
list.next = null;
Node temp;
while(next !=null){
temp = next.next;
next.next = head;
head = next;
next = temp;
}
list = head;
}
c语言用单链表实现lru算法,手写单链表实现和LRU算法模拟相关推荐
- 算法------手写LRU算法
算法------手写LRU算法 LRU是Redis中常用的内存淘汰算法. 意思是:当缓存容量满的时候,淘汰最近很少使用的数据. 具体实现逻辑: 把缓存放到双向链表当中,最近使用过.或新放入的缓存,放到 ...
- 手写识别底层原理_LinkedList底层原理和手写单链表
2.1 单链表技能点 · 认识单链表 o 特点 数据元素的存储对应的是不连续的存储空间,每个存储结点对应一个需要存储的数据元素. 每个结点是由数据域和指针域组成. 元素之间的逻辑关系通过存储节点之间的 ...
- c语言 双向链表增删修查,手写双链表,并实现增删改查
手写双链表,并实现增删改查 public class DoublyLinkedListT { // 一个空的头节点 private final Node head = new Node(null); ...
- 课程设计(毕业设计)—基于机器学习KNN算法手写数字识别系统—计算机专业课程设计(毕业设计)
机器学习KNN算法手写数字识别系统 下载本文手写数字识别系统完整的代码和课设报告的链接(或者可以联系博主koukou(壹壹23七2五六98),获取源码和报告):https://download.csd ...
- Python 手写机器学习最简单的 kNN 算法
https://www.toutiao.com/a6698919092876739079/ Python 手写机器学习最简单的 kNN 算法 苏克1900 Python爬虫与数据挖掘 本文 3000 ...
- 应对笔试手写代码,如何准备深度优先算法 广度优先算法?
应对笔试手写代码,如何准备深度优先算法 & 广度优先算法? 1. 什么是深度优先算法?什么又是广度优先算法? 2. 广度优先算法使用场景 3. 广度优先算法模板 4. 深度优先算法使用场景 5 ...
- java面试手写单链表_(转)面试大总结之一:Java搞定面试中的链表题目
packageLinkedListSummary; importjava.util.HashMap; importjava.util.Stack; /** * http://blog.csdn.net ...
- 20个经典数据结构与算法,300多幅算法手绘图解,带你领略算法之美
一些经典的数据结构和算法图书,偏重理论,读者学起来可能感觉比较枯燥.一些趣谈类的数据结构和算法图书,虽然容易读懂,但往往内容不够全面.另外,很多数据结构和算法图书缺少真实的开发场景,读者很难将理论和实 ...
- javascript算法+手写js面试题
链表 function ListNode(val, next) {this.val = (val===undefined ? 0 : val)this.next = (next===undefined ...
最新文章
- python语言自学-python语言学习笔记整理
- mysql @@version_MySQL数据库安装Version5.5
- Android Fragments 详细使用
- 松下机器人找原点步骤_桁架机器人在汽车座椅安装生产线中的应用
- 使用Word 2007写blog
- 关于如何安装cocoapods
- 教师职称考计算机模块,2015教师职称计算机考试模块.doc
- 工具------Java反编译工具XJad
- 计算机操作系统|汤小丹|第四版|习题答案(五)
- 文件搜素神器-everthing 快
- 第十九届泳联水中运动世锦赛
- 专属圣诞树+2022跨年HTML(腾讯云部署)
- 电石炉技术的发展及电石炉尾气解决方案
- 四川山海蓝图抖音账号权重分类
- 建立远程桌面需要在对方服务器上做设置吗,SFB 项目经验-23-要远程登录,你需要具有通过远程桌面服务进行登录的权限...
- ( 算法树之几何 )【 皮克定理 】
- Ask and Answer
- 【mysql】mysql查询优化之索引类型、最左前缀
- 发现一个优秀的Java版国人仿曹操传游戏 1
- xshell如何复制粘贴外面的内容?
热门文章
- linux多线程求和_Linux下使用两个线程协作完成一个任务的简易实现
- html css分页特效,js css自定义分页效果
- 2017计算机考研视频百度云盘,2017年数学考研视频
- 什么是PunkBuster,我可以卸载它吗?
- 爬虫从入门到入预(8)
- OMNeT++ SUMO 学习记录(六)SUMO 路网生成及简单仿真实例
- C语言练习之计算存款利息
- 利用squid的tcp_outgoing_address特性选择第二块网卡出流量( by quqi99)
- 拜占庭将军问题之口头协议
- 行业:DP2.0接口性能逆天,但想要普及还很难