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

LRU的C++的简单实现

时间:2014-08-21 00:17:53      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:c++   数据结构   

class LRUCache提供两个接口:get(int key)和set(int key,value)

#include<iostream>


using namespace std;






class LRUCache{
public:
    LRUCache(int cap):current(0),capacity(cap){
        A=new node[cap];
    }
    int get(int key) {
        for(int i=0;i<current;i++)
        {
        <span style="white-space:pre">	</span>if(key==A[i].key)
        <span style="white-space:pre">	</span>{
        <span style="white-space:pre">		</span>int temp=A[i].value;
        <span style="white-space:pre">		</span>node tt=A[i];
        <span style="white-space:pre">		</span>for(int k=i;k<current-1;k++)
        <span style="white-space:pre">		</span>{
        <span style="white-space:pre">			</span>A[k]=A[k+1];
        <span style="white-space:pre">		</span>}
        <span style="white-space:pre">		</span>A[current-1]=tt;
        <span style="white-space:pre">		</span>return temp;
        <span style="white-space:pre">	</span>}
        }
        return -1;
    }
  void set(int key, int value) {
    <span style="white-space:pre">	</span>    node t;
        <span style="white-space:pre">	</span>t.key=key;
        <span style="white-space:pre">	</span>t.value=value;
        <span style="white-space:pre">	</span>bool has=false;
        for(int i=0;i<current;i++)
        {
        <span style="white-space:pre">	</span>if(key==A[i].key)
        <span style="white-space:pre">	</span>{
<span style="white-space:pre">			</span>  A[i].value=value;
<span style="white-space:pre">			</span>  node tt=A[i];
<span style="white-space:pre">			</span>  for(int k=i;k<current-1;k++)
        <span style="white-space:pre">		</span>{
        <span style="white-space:pre">			</span>A[k]=A[k+1];
        <span style="white-space:pre">		</span>}
        <span style="white-space:pre">		</span>A[current-1]=tt;
        <span style="white-space:pre">		</span>has=true;
        <span style="white-space:pre">		</span>break;
<span style="white-space:pre">			</span>}
        }
        if(!has)
        {
       
        if(current<capacity)
        {
        <span style="white-space:pre">	</span>A[current++]=t;
        }else{
        <span style="white-space:pre">	</span>for(int k=0;k<current-1;k++)
        <span style="white-space:pre">		</span>{
        <span style="white-space:pre">			</span>A[k]=A[k+1];
        <span style="white-space:pre">		</span>}
        <span style="white-space:pre">		</span>A[current-1]=t;
        }
         }
        print();
    }
    void print()
    {
    <span style="white-space:pre">	</span>for(int i=0;i<current;i++)
    <span style="white-space:pre">	</span>cout<<A[i].key<<" ";
        cout<<endl;
    }
private:
    int current;
    int capacity;
    struct node{
<span style="white-space:pre">	</span>int key;
<span style="white-space:pre">	</span>int value;
    };
    void swap(node &a,node &b)
    {
<span style="white-space:pre">	</span>node temp;
<span style="white-space:pre">	</span>temp=a;
<span style="white-space:pre">	</span>a=b;
<span style="white-space:pre">	</span>b=temp;
   }
   node *A;
};
int main()
{
<span style="white-space:pre">	</span>LRUCache lru(4);
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>lru.set(2,1);
<span style="white-space:pre">	</span>lru.set(2,2);
<span style="white-space:pre">	</span>lru.set(3,2);
<span style="white-space:pre">	</span>lru.set(4,2);
<span style="white-space:pre">	</span>lru.set(5,2);
<span style="white-space:pre">	</span>lru.set(1,2);
<span style="white-space:pre">	</span>lru.set(4,2);
<span style="white-space:pre">	</span>lru.set(3,2);
<span style="white-space:pre">	</span>lru.set(4,2);
<span style="white-space:pre">	</span>lru.get(1);
<span style="white-space:pre">	</span>lru.print();
<span style="white-space:pre">	</span>
}
测试结果:




LRU的C++的简单实现,布布扣,bubuko.com

LRU的C++的简单实现

标签:c++   数据结构   

原文地址:http://blog.csdn.net/igiqoanw/article/details/38715371

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