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> }测试结果:
原文地址:http://blog.csdn.net/igiqoanw/article/details/38715371