LRU Cache
2015-07-24 16:39
337 查看
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:
getand
set.
get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value)- Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
struct node{ int key; int value; node* pre; node* next; }; class LRUCache{ private: int count; int size; map<int,node*> mp; node* cachehead; node* cachetail; public: LRUCache(int capacity) { count=0; size=capacity; cachehead=NULL; cachetail=NULL; } int get(int key) { if(cachehead==NULL) return -1; map<int,node*>::iterator it=mp.find(key); if(it==mp.end()) return -1; else { node* p=it->second; pushfront(p); //p->pre->next=p->next; /*if(p->next!=NULL) p->next->pre=p->pre; mp.erase(p->key); p->next=cachehead; p->pre=cachehead->pre; cachehead->pre=p; cachehead=cachehead->pre; mp[cachehead->key]=cachehead;*/ } return cachehead->value; } void set(int key, int value) { if(cachehead==NULL) { cachehead=(node*)malloc(sizeof(node)); cachehead->key=key; cachehead->value=value; cachehead->pre=NULL; cachehead->next=NULL; mp[key]=cachehead; cachetail=cachehead; count++; } else { map<int,node*>::iterator it=mp.find(key); if(it==mp.end()) { if(count==size) { if(cachehead==cachetail&&cachehead!=NULL) { mp.erase(cachehead->key); cachehead->key=key; cachehead->value=value; mp[key]=cachehead; } else { node* p=cachetail; cachetail->pre->next=cachetail->next; cachetail=cachetail->pre; mp.erase(p->key); p->key=key; p->value=value; p->next=cachehead; p->pre=cachehead->pre; cachehead->pre=p; cachehead=p; mp[cachehead->key]=cachehead; } } else { node* p=(node*)malloc(sizeof(node)); p->key=key; p->value=value; p->next=cachehead; p->pre=cachehead->pre; cachehead->pre=p; cachehead=p; mp.erase(p->key); mp[cachehead->key]=cachehead; count++; } } else { node* p=it->second; p->value=value; pushfront(p); /* mp.erase(p->key); p->pre->next=p->next; if(p->next!=NULL) p->next->pre=p->pre; p->value=value; p->next=cachehead; p->pre=cachehead->pre; cachehead->pre=p; cachehead=cachehead->pre; mp[cachehead->key]=cachehead;*/ } } } void pushfront(node* cur) { if(count==1) return; if(cur==cachehead) return; if(cur==cachetail) cachetail=cur->pre; cur->pre->next=cur->next; if(cur->next!=NULL) cur->next->pre=cur->pre; cur->next=cachehead; cur->pre=cachehead->pre; cachehead->pre=cur; cachehead=cur; } };
相关文章推荐
- Objective-c的MD5/SHA1加密算法的实现
- hdu 2050 折线分割平面
- python文件操作
- 1040 统计单词个数
- String方法汇总
- CImage
- Java(String) 常用字符串函数
- 开发杂记
- Java SPI 实例
- MySQL数据库服务器安装标准
- 使用Apache commons的FileUtils类读取文本文件内容到字符串
- Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard'
- IOS中使用单例的简单介绍
- ListView的用法
- 计蒜客第28题---等和的分割子集
- HDU 1068 Girls and Boys【最大独立集】
- windows 网络参数设置
- java从键盘输入数据的两种方法
- jquery ajax备用
- 剑指OFFER || 打印从1到最大的N位数 12_Print1ToMaxOfNDigits.cpp