最近最久未使用(LRU)置换算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <assert.h> using namespace std;
const int Maxn = ; typedef struct Page_Node{
struct Page_Node *next;
int page_id;
Page_Node(int v){page_id = v;}
}*PNode, Node; class LRU{
public:
LRU(int num):blocks_limit(num){
head = NULL;
} ~LRU(){
delete head;
} void update_order(int page_id){
PNode p = head, last_node = NULL;
while(p){
if(p -> page_id == page_id){
PNode next_node = p -> next;
p -> next = head;
if(last_node){
last_node -> next = next_node;
}head = p;
break;
}
last_node = p;
p = p -> next;
}
} void insert_page(int page_id){
PNode new_node = new Node(page_id);
PNode tmp = head;
head = new_node;
head -> next = tmp;
existed[page_id] = ;
} void delete_tail_page(){
assert(NULL != head);
PNode p = head, last_node = NULL;
while(p){
if(p -> next == NULL){
existed.erase(p->page_id);
if(last_node)
last_node -> next = NULL;
else{
head = NULL;
}
}
last_node = p;
p = p -> next;
}
} void disp_page_in_memory(){
cout<<"页面情况:"<<endl;
PNode p = head;
while(p){
cout<<" "<<p->page_id;
p = p -> next;
}cout<<endl;
} void call_page(int page_id){
if(existed[page_id]){
cout<<"In Memory"<<endl;
update_order(page_id);
}else{
cout<<"Out of Memory"<<endl;
if(existed.size() > blocks_limit){
cout<<"页面置换"<<endl;
delete_tail_page();
insert_page(page_id);
}else{
cout<<"直接调页"<<endl;
insert_page(page_id);
}
}
disp_page_in_memory();
} private:
PNode head;
map<int, int>existed;
int blocks_limit;
}; int main()
{
LRU lru();
while(){
int t;
cin>>t;
lru.call_page(t);
} return ;
}

最少使用次数(LFU)置换算法

先进先出置换算法(FIFO)

最新文章

  1. 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
  2. 未能加载文件或程序集“System.Web.DataVisualization...”
  3. input输入内容时放大问题
  4. jQuery UI--jquery-autohide解读
  5. 2016.7.13abstract
  6. C#中创建、打开、读取、写入、保存Excel的一般性代码
  7. SGU131--NYOJ435
  8. linux命令-查看当前目录下所有文件的大小:“ll -h”
  9. HTML5新标签
  10. 道破Redis的VM
  11. kairosdb + cassandra Setup
  12. Eclipse导入项目常见问题----服务器版本问题02
  13. dotweb框架之旅 [二] - 常用对象-App(dotweb)
  14. WordPress彩色背景标签云实现
  15. 为什么目前无法再docker for windows中调用GPU
  16. Android中自定义广播的实现
  17. (转)mybatis数据库物理分页插件PageHelper
  18. docker安装指定版本TAG的镜像
  19. 001-mybatis框架
  20. 相关TableLayoutPanel分页显示自定义控件

热门文章

  1. UVA 674_Coin Change
  2. [bzoj3196][Tyvj1730]二逼平衡树_树套树_位置线段树套非旋转Treap/树状数组套主席树/权值线段树套位置线段树
  3. win10笔记本相机打开黑屏无法打开笔记本相机
  4. Ubuntu 16.04添加多张虚拟网卡
  5. 【转】配置windows路由表,使电脑同时连接内网外网方法
  6. 在对象内部尽量直接訪问实例变量 --Effictive Objective-C 抄书
  7. VMWare中的Host-only、NAT、Bridge的比較
  8. web 开发之js---理解并解决IE的内存泄漏方式
  9. git 在一台机器上配置多个账户
  10. 在js中取选中的radio值