简介

链表就是链式存储数据的一种数据结构。双向链表每个数据存储都包含他的前后数据节点的位置信息(索引/指针)。

   class DSChain<T>
{
//使用栈来进行废弃空间回收
private DSStack<int> _recycle; //数据需要三个数据来存储内容,分别存储前后节点索引和数据本身
private int[] _prev;
private T[] _ds;
private int[] _next; //链表头尾的索引,跟踪表尾主要方便LRU使用
private int _head;
private int _tail; public DSChain(int length)
{
_head = -1;
_tail = -1;
_prev = new int[length];
_ds = new T[length];
_next = new int[length]; _recycle = new DSStack<int>(length);
//将所有可用空间压入栈,也可改良当顺序空间耗尽后再读取栈中记录的回收空间
for (int i = 0; i < length; i++)
{
_recycle.Push(i);
}
} //搜索数据,返回所在索引
int Search(T data)
{
if (_head == -1) return -1;
int index = _head;
while (!_ds[index].Equals(data))
{
index = _next[index];
if (index == -1) return -1;
}
return index;
} public bool Insert(T data)
{
int index;
if (!_recycle.Pop(out index)) return false;
if (_head == -1)
{
_prev[index] = -1;
_ds[index] = data;
_next[index] = -1;
_tail = index;
}
else
{
_prev[index] = -1;
_ds[index] = data;
_next[index] = _head;
_prev[_head] = index;
}
_head = index;
return true;
} public bool Delete(T data)
{
int index = Search(data);
if (index == -1) return false; if (_prev[index] != -1) _next[_prev[index]] = _next[index];
else _head = _next[index]; if (_next[index] != -1) _prev[_next[index]] = _prev[index];
else _tail = _prev[index]; _recycle.Push(index);
return true;
} //LRU
public bool DeleteLast()
{
int index = _tail;
if (index == -1) return false;
_tail = _prev[index];
_next[_prev[index]] = -1;
_recycle.Push(index);
return true;
} //LRU访问方法,读取数据的同时调整数据在链表中位置
//链表这种数据结构实现LRU非常方便
public bool LRUAccess(T data)
{
int index = Search(data);
if (index == -1) return false;
if (_head == index) return true;
if (_tail == index) _tail = _prev[index]; _next[_prev[index]] = _next[index];
if (_next[index] != -1) _prev[_next[index]] = _prev[index]; _prev[index] = -1;
_next[index] = _head;
_prev[_head] = index;
_head = index;
return true;
}
}

链表&LRU的更多相关文章

  1. 单链表LRU

    单链表实现lru 越靠近链表尾部的节点是越早之前访问的 当有一个新的数据被访问时,从链表头开始顺序遍历链表 1.如果此数据之前已经被缓存在链表中 遍历得到这个数据对应的节点,并将其从原来的位置删除,然 ...

  2. 再谈LRU双链表内存管理

    N年前我写了个双链表也发了博客,还添了代码.但是那个代码不但复杂,而且还有有问题的,一直懒得整理,放在空间误导别人.最近在写服务端,今天抽点空补一篇. 关于LRU网上随便搜,有过后端经验的人应该很多都 ...

  3. 你知道MySQL的LRU链表吗?

    相信大家对LRU链表是不陌生的,算是一种基础的数据结构! LRU:Least Recently Used 一.简述传统的LRU链表 LRU:Least Recently Used 相信大家对LRU链表 ...

  4. OCP读书笔记(13) - 管理内存

    SGA 1. 什么是LRULRU表示Least Recently Used,也就是指最近最少使用的buffer header链表LRU链表串联起来的buffer header都指向可用数据块 2. 什 ...

  5. Linux内存管理 (13)回收页面

    专题:Linux内存管理专题 关键词:LRU.活跃/不活跃-文件缓存/匿名页面.Refault Distance. 页面回收.或者回收页面也即page reclaim,依赖于LRU链表对页面进行分类: ...

  6. Linux内核设计笔记13——虚拟文件系统

    虚拟文件系统 内核在它的底层文件系统系统接口上建立一个抽象层,该抽象层使Linux可以支持各种文件系统,即便他们在功能和行为上存在很大差异. VFS抽象层定义了各个文件系统都支持的基本的.概念上的接口 ...

  7. 800+Java后端经典面试题,希望你找到自己理想的Offer呀~

    前言 在茫茫的互联网海洋中寻寻觅觅,我收藏了800+道Java经典面试题,分享给你们.建议大家收藏起来,在茶余饭后拿出来读一读,以备未雨绸缪之需.另外,面试题答案的话,我打算后面慢慢完善在github ...

  8. MySQL InnoDB技术内幕:内存管理、事务和锁

    前面有多篇文章介绍过MySQL InnoDB的相关知识,今天我们要更深入一些,看看它们的内部原理和机制是如何实现的. 一.内存管理 我们知道,MySQl是一个存储系统,数据最后都写在磁盘上.我们以前也 ...

  9. Innodb的存储及缓存

    参考[mysql技术内幕] 一.mysql体系结构和存储引擎 1.数据库与数据库实例 数据库:物理操作系统文件或者其他文件组成的集合: 数据库实例:有数据库后台进程/线程和一个共享内存区域组成. 数据 ...

随机推荐

  1. 《Android NFC 开发实战详解 》简介+源码+样章+勘误ING

    <Android NFC 开发实战详解>简介+源码+样章+勘误ING SkySeraph Mar. 14th  2014 Email:skyseraph00@163.com 更多精彩请直接 ...

  2. Unity IOS Build的Graphics API最好是固定Opengl ES 2.0

    不要选择Automatic也不要选择Metal,因为这个选项可能会导致app在Iphone6上出现crash. 一个类似的crash堆栈: http://stackoverflow.com/quest ...

  3. ABP集合贴(转)

    ABP集合贴 本文背景 公司最近规划的新框架准备基于ABP来搭建,自从在阳铭博客看到ABP框架的介绍后,就一直持续关注着,但还没真正在实际项目中直接使用ABP,只是自己做了一些学习和Demo.ABP所 ...

  4. Node.js之NPM工具使用

    1.NPM介绍:包管理工具 (1)允许用户从NPM服务器下载别人编写的第三方包到本地石使用 (2)允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用 (3)允许用户将自己编写的包或命令行程 ...

  5. 【oracle】union、union all、intersect、minus 的用法及区别

    一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...

  6. [转] js对象浅拷贝和深拷贝详解

    本文为大家分享了JavaScript对象的浅拷贝和深拷贝代码,供大家参考,具体内容如下 1.浅拷贝 拷贝就是把父对像的属性,全部拷贝给子对象. 下面这个函数,就是在做拷贝: var Chinese = ...

  7. linux下的tcp连接超时

    最近需要写一个linux下的通信程序, 通信模块用的是Qt的QTcpSocket. 最后程序需要增加一个断网检测, 在windows下调试没问题, 拔网线, 断网口都能马上检测到, 但到了部署到lin ...

  8. MVC项目中ExecutionTimeout不生效的解决方案

    我们做web服务器端开发时,经常会遇到一个需求场景,因为某些耗时处理造成页面的响应处理时间超长,技术角度就想能否给页面处理程序一个指定的超时时间,服务端处理程序执行时间超过这个指定的超时时间则中断处理 ...

  9. About_全在里面

    分享·地址:http://www.itxueyuan.org/view/6254.html

  10. UITextView 点击添加文字 光标处于最后方

    #import "ViewController.h" @interface ViewController ()<UITextViewDelegate> @end @im ...