Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

LRU是虚拟内存技术中,页置换时需要用到的算法,最近最少使用算法。也就是说替换掉最近最少被使用的页。

采用双链表实现一个栈,栈顶(用head指针表示)放最近使用的元素。end指针表示栈底,地方不够的时候end指针的元素值就会被覆盖,覆盖后因为成了最近使用,所以会被提到head的位置。链表中部也可能有元素被提到head的位置。

class LRUCache{
struct ListNode{
int key;
int value;
ListNode* prev;
ListNode* next;
};
public:
LRUCache(int capacity){
if(capacity >= ){
Capacity = capacity;
size = ;
}
} void set(int key, int value){
ListNode* p = head;
for(; p != NULL && p -> key != key; p = p -> next);
if(p == NULL && size == Capacity){ //The case that didn't find key and capacity is full
p = end;
p -> key = key;
}
if(p != NULL){ //The case that has found the key (treat the previous case as found the key in end)
p -> value = value;
if(p != head){ //If the key is in head of the list, we don't need to do anything since the head is the most recently used.
ListNode* pre = p -> prev;
ListNode* fol = p -> next; if(p == end && pre != NULL){
end = pre;
}
p -> next = head;
p -> prev = NULL;
head -> prev = p;
head = p; if(pre != NULL)
pre -> next = fol;
if(fol != NULL)
fol -> prev = pre;
}
}else{ //The case that has not found the key, and capacity is not full, need to create one.
p = new ListNode();
p -> key = key;
p -> value = value;
if(head == NULL){
head = end = p;
p -> prev = NULL;
p -> next = NULL;
}else{
p -> prev = NULL;
p -> next = head;
head -> prev = p;
head = p;
}
size++;
} } int get(int key){
ListNode* p = head;
for(; p != NULL && p -> key != key; p = p -> next);
if(p == NULL) return -; if(p != head){
ListNode* pre = p -> prev;
ListNode* fol = p -> next;
if(p == end)
end = pre; p -> next = head;
p -> prev = NULL;
head -> prev = p;
head = p; if(pre != NULL)
pre -> next = fol;
if(fol != NULL)
fol -> prev = pre;
}
return p -> value;
}
private:
ListNode* head = NULL;
ListNode* end = NULL;
int size = ;
int Capacity = ;
};

最新文章

  1. 前端学HTTP之安全HTTP
  2. 从零开始,DIY一个jQuery(3)
  3. [CORS:跨域资源共享] W3C的CORS Specification
  4. 基于FPGA的电压表与串口通信(上)
  5. 【收藏】android WebView总结
  6. A20VGA和lvds显示的切换-
  7. openjudge 大师兄,师傅被妖怪抓走啦
  8. hdu 4381(背包变形)
  9. 使用WebView显示网页
  10. C实现二叉树(模块化集成,遍历的递归与非递归实现)
  11. EL表达式处理字符串 是否 包含 某字符串 截取 拆分...............
  12. Head First C 笔记
  13. Solidity调试 - 实现变量打印
  14. [百度百科]dir命令指定显示的排序方式
  15. SSM-Netty实现软硬件通信,真实项目案例
  16. [C#] LINQ之LookUp
  17. 催希凡javaweb 学习28天
  18. C# 日志输出工具库—log4net 安装、配置及简单应用
  19. 深入聊聊Java多线程
  20. Android访问远程网页取回json数据

热门文章

  1. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
  2. iostat lsof
  3. Thrift IDL使用方式
  4. 买卖股票的最佳时机I II III IV
  5. ACM 第十八天
  6. .net控制台程序Program args参数解析
  7. Vue于React特性简单对比(一)
  8. Mysql查询优化从入门到跑路(三)查询的基本操作
  9. change object keys & UpperCase & LowerCase
  10. 使用js 复制 文字到剪贴板