LRU Cache的C++实现
来源:互联网 发布:ember.js入门教程 编辑:程序博客网 时间:2024/05/18 00:55
方法一:双链表+哈希表map
#include<iostream>#include<map>using namespace std;struct Node{ int key; int value; Node* pre; Node* next; Node(int key=-1,int value=-1):key(key),value(value),pre(NULL),next(NULL){};};class Solution{private: int capacity; Node* head; Node* tail; map<int,Node*>cachemp;public: Solution(int size=5); ~Solution(); void deletenode(Node* node); void putfront(Node* node); int get(int key); void set(int key,int value); void display(); void showmap();};Solution::Solution(int size){ capacity=size; head=new Node; tail=new Node; head->next=tail; head->pre=NULL; tail->pre=head; tail->next=NULL;}Solution::~Solution(){ Node* p; p=head->next; while(p) { delete(p->pre); p=p->next; } delete(tail);} void Solution::putfront(Node* node){ node->next=head->next; head->next=node; node->next->pre=node; node->pre=head; } void Solution::deletenode(Node* node) { if(node==NULL) return; node->pre->next=node->next; node->next->pre=node->pre; delete(node); } int Solution::get(int key) { map<int,Node*>::iterator iter=cachemp.find(key); if(iter==cachemp.end()) { return -1; } else { Node* node=new Node(iter->second->key,iter->second->value); deletenode(iter->second); cachemp.erase(iter); putfront(node); cachemp[key]=node; return node->value; } showmap(); } void Solution::set(int key,int value) { map<int,Node*>::iterator iter=cachemp.find(key); Node* node=new Node(key,value); if(iter==cachemp.end()) { if(cachemp.size()==capacity) { cachemp.erase(tail->pre->key); deletenode(tail->pre); } putfront(node); cachemp[key]=node; } else { deletenode(iter->second); putfront(node); cachemp.erase(iter); cachemp[key]=head->next; } display(); showmap(); } void Solution::display() { Node* p=head->next; cout<<"*********"<<endl; cout<<"realsize="<<cachemp.size()<<endl; while(p!=tail) { cout<<p->key<<' '<<p->value<<endl; p=p->next; } } void Solution::showmap() { cout<<"***"<<endl; for(map<int,Node*>::iterator iter=cachemp.begin();iter!=cachemp.end();iter++) cout<<iter->first<<' '<<iter->second->key<<' '<<iter->second->value<<endl; } void main() { Solution solution(3); solution.set(-1,-1); solution.set(2,2); solution.set(3,3); solution.set(4,4); cout<<"print"<<solution.get(4)<<endl; cout<<"print"<<solution.get(2)<<endl; cout<<"print"<<solution.get(3)<<endl; cout<<"print"<<solution.get(1)<<endl; solution.set(2,9); solution.set(3,14); solution.set(4,11); solution.set(5,5); cout<<"print"<<solution.get(1)<<endl; cout<<"print"<<solution.get(2)<<endl; cout<<"print"<<solution.get(3)<<endl; cout<<"print"<<solution.get(4)<<endl; cout<<"print"<<solution.get(5)<<endl; }
方法二:双链表
#include<iostream>using namespace std;struct Node{ int key; int value; Node* pre; Node* next; Node(int key=-1,int value=-1):key(key),value(value),pre(NULL),next(NULL){};};class Solution{private: int capacity; int realsize; Node* head; Node* tail;public: Solution(int size=10); ~Solution(); int get(int key); void set(int key,int value); void putfront(Node* node); void deletenode(Node* node); void display();};Solution::Solution(int size){ realsize=0; capacity=size; head=new Node; tail=new Node; head->next=tail; head->pre=NULL; tail->pre=head; tail->next=NULL;}Solution::~Solution(){ Node* p; p=head->next; while(p) { delete(p->pre); p=p->next; } delete(tail);} void Solution::putfront(Node* node){ node->next=head->next; head->next=node; node->next->pre=node; node->pre=head; } void Solution::deletenode(Node* node) { node->pre->next=node->next; node->next->pre=node->pre; delete(node); } int Solution::get(int key) { Node* node=head->next; while(node!=tail) { if(node->key==key) { Node* node1=new Node(node->key,node->value); deletenode(node); putfront(node1); return node1->value; } node=node->next; } return -1; } void Solution::set(int key,int value) { Node* node=head->next; Node* node1=new Node(key,value); while(node!=tail) { if(node->key==key) { deletenode(node); putfront(node1); display(); return ; } node=node->next; } if(realsize==capacity) { deletenode(tail->pre); putfront(node1); } else { putfront(node1); realsize++; } display(); } void Solution::display() { Node* p=head->next; cout<<"*********"<<endl; cout<<"realsize="<<realsize<<endl; while(p!=tail) { cout<<p->key<<' '<<p->value<<endl; p=p->next; } } void main() { Solution solution(3); solution.set(-1,-1); solution.set(2,2); solution.set(3,3); solution.set(4,4); cout<<"print"<<solution.get(4)<<endl; cout<<"print"<<solution.get(2)<<endl; cout<<"print"<<solution.get(3)<<endl; cout<<"print"<<solution.get(1)<<endl; solution.set(2,9); solution.set(3,14); solution.set(5,5); cout<<"print"<<solution.get(1)<<endl; cout<<"print"<<solution.get(2)<<endl; cout<<"print"<<solution.get(3)<<endl; cout<<"print"<<solution.get(4)<<endl; cout<<"print"<<solution.get(5)<<endl; }
方法三:双链表+哈希表map
#include<iostream>#include<map>#include<list>using namespace std;struct Node{ int key; int value; Node(int key=-1,int value=-1):key(key),value(value){}};class cache{private: int capacity; list<Node>listnode; map<int,list<Node>::iterator>cachelist; void display();public: cache(int num):capacity(num){}; int get(int); void set(int key,int value);};void cache::set(int key,int value){ Node node; map<int,list<Node>::iterator>::iterator iter=cachelist.find(key); if(iter==cachelist.end()) { if(cachelist.size()==capacity) { cachelist.erase(cachelist.find(listnode.back().key)); listnode.pop_back(); } node.key=key; node.value=value; listnode.push_front(node); cachelist[key]=listnode.begin(); } else { listnode.erase(iter->second); node.key=key; node.value=value; listnode.push_front(node); cachelist[key]=listnode.begin(); } display();}int cache::get(int key){ Node *node=new Node; map<int,list<Node>::iterator>::iterator iter=cachelist.find(key); if(iter==cachelist.end()) { return -1; } else { node->key=key; node->value=iter->second->value; listnode.erase(iter->second); listnode.push_front(*node); cachelist.erase(iter); cachelist[key]=listnode.begin(); } display(); return node->value;}void cache::display(){ cout<<"*******"<<endl; for(list<Node>::iterator it=listnode.begin();it!=listnode.end();it++) cout<<it->key<<' '<<it->value<<endl; cout<<"###"<<endl; for(map<int,list<Node>::iterator>::iterator ti=cachelist.begin();ti!=cachelist.end();ti++) cout<<ti->first<<' '<<cachelist[ti->first]->key<<' '<<cachelist[ti->first]->value<<endl;}void main(){ cache Cache(3); Cache.set(1,1); Cache.set(2,2); Cache.set(3,3); Cache.set(4,4); cout<<"print"<<Cache.get(4)<<endl; cout<<"print"<<Cache.get(2)<<endl; cout<<"print"<<Cache.get(3)<<endl; cout<<"print"<<Cache.get(1)<<endl; Cache.set(2,9); Cache.set(3,14); Cache.set(4,11); Cache.set(5,5); cout<<"print"<<Cache.get(1)<<endl; cout<<"print"<<Cache.get(2)<<endl; cout<<"print"<<Cache.get(3)<<endl; cout<<"print"<<Cache.get(4)<<endl; cout<<"print"<<Cache.get(5)<<endl; }
0 0
- LRU cache的实现
- LRU Cache的实现
- LRU Cache的实现
- LRU Cache的实现
- LRU的cache的实现
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- LRU cache的简单实现
- LRU Cache的一种实现
- LRU cache的另一种实现
- LRU Cache的C++实现
- LRU Cache的简单实现
- LRU cache 算法的实现
- Cache的设计和实现 LRU Cache
- Cache的设计和实现 LRU Cache
- 用LinkedHashMap实现的简单LRU Cache
- [安卓]新闻客户端(一)Splash
- 使用Filter防止浏览器缓存页面或请求结果
- 类模板
- 字符串最短距离问题
- [安卓]新闻客户端(二)引导页
- LRU Cache的C++实现
- Vim 练级
- 安卓获取来去电号码
- (七十)Xcode5及以上对于状态栏和导航栏样式的设定方法
- 黑马程序员——引入头文件
- Just Today
- 详解CSS选择器、优先级与匹配原理
- 【Ogre编程入门与进阶】第十六章 GPU编程
- 关于STM32影子寄存器和预装载寄存器和TIM_ARRPreloadConfig