码迷,mamicode.com
首页 > 编程语言 > 详细

LRU(Least Recently Used最近最少使用)的c++实现(顺序表)

时间:2016-06-16 16:15:59      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

最近重修程序设计能力,有一句话对我的修行计划有很好的指导,能力(知识+思考+实践),三项中缺乏最多的就是实践,也是最耗时的。花了一早上写的一个简单的LRU程序

问题的抽象如下图:默认尾部为刚进入cache,头部为最近最久未使用

技术分享

 

  1 //代码默认的cache有3块大小
  2 
  3 #include <iostream>
  4 
  5 using namespace std;
  6 
  7 //定义cache类,实现顺序表cache以及LRU的用到了查找,删除和插入操作
  8 class cache
  9 {
 10 public:
 11     int arr[3];
 12     int arrlen;
 13     int current;
 14 
 15     cache()
 16     {
 17         arr[0] = 0;
 18         arr[1] = 0;
 19         arr[2] = 0;
 20 
 21         arrlen = 0;
 22         current = 0;
 23     }
 24 
 25     int findNode(int value)   //查找节点
 26     {
 27         if(arrlen == 0)
 28         {
 29             cout << "cache为空" << endl;
 30             return -1;
 31         }
 32         for(int i = 0; i < 3; ++i)
 33         {
 34             if(arr[i] == value)
 35             {
 36                 current = i;
 37                 return 1;
 38             }
 39         }
 40         return 0;
 41 
 42     }
 43     void deleteNode(int position) //删除节点(current)
 44     {
 45         if(arrlen == 0)
 46         {
 47             cout << "数组空,删除无效" << endl;
 48             return ;
 49         }
 50         if(position > 2)
 51         {
 52             cout << "删除位置不合法" << endl;
 53             return ;
 54         }
 55         for(int i = position; i < position; ++i)
 56         {
 57             arr[i] = arr[i+1];
 58         }
 59         arrlen--;
 60 
 61     }
 62     void insertNode(int valuecc)  //插入节点(尾)
 63     {
 64         if(arrlen != 0)
 65         {
 66             for(int i = 2; i > 0; --i)
 67             {
 68                 arr[i] = arr[i-1];
 69             }
 70         }
 71         //bank *node= new bank;
 72         arr[0] = valuecc;
 73         arrlen = arrlen + 1;
 74     }
 75 };
 76 int main()
 77 {
 78     cache cc;
 79     int value;
 80     while(cin >> value)
 81     {
 82         if(cc.arrlen < 3)
 83         {
 84             if(cc.findNode(value) == 1)
 85             {
 86                 cc.deleteNode(cc.current);
 87                 cc.insertNode(value);
 88             }
 89             else
 90             {
 91                 cc.insertNode(value);
 92             }
 93 
 94         }
 95         else
 96         {
 97             if(cc.findNode(value) == 1)
 98             {
 99                 cc.deleteNode(cc.current);
100                 cc.insertNode(value);
101             }
102             else
103             {
104                 cc.current = 2;
105                 cc.deleteNode(cc.current);
106                 cc.insertNode(value);
107             }
108         }
109     }
110     for(int i = 0; i <= 2; i++)
111     {
112         cout << cc.arr[i] << endl;
113     }
114     return 0;
115 }

 

LRU(Least Recently Used最近最少使用)的c++实现(顺序表)

标签:

原文地址:http://www.cnblogs.com/fengjl/p/5591213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!