LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and 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.

  1 struct node{
  2     int key;
  3     int value;
  4     node* pre;
  5     node* next;
  6 };
  7 class LRUCache{
  8 private:
  9      int count;
 10      int size;
 11      map<int,node*> mp;
 12      node* cachehead;
 13      node* cachetail;
 14 public:
 15     LRUCache(int capacity) {
 16         count=0;
 17         size=capacity;
 18        
 19         cachehead=NULL;
 20         cachetail=NULL;
 21     }
 22     
 23     int get(int key) {
 24         if(cachehead==NULL) return -1;
 25         map<int,node*>::iterator it=mp.find(key);
 26         if(it==mp.end())
 27             return -1;
 28         else
 29         {
 30             node* p=it->second;
 31             pushfront(p);
 32                 //p->pre->next=p->next;
 33                 /*if(p->next!=NULL)
 34                     p->next->pre=p->pre;
 35                 mp.erase(p->key);
 36                 p->next=cachehead;
 37                 p->pre=cachehead->pre;
 38                 cachehead->pre=p;
 39                 cachehead=cachehead->pre;
 40                 mp[cachehead->key]=cachehead;*/
 41             
 42         }
 43         return cachehead->value;
 44     }
 45     
 46     void set(int key, int value) {
 47         if(cachehead==NULL)
 48         {
 49             cachehead=(node*)malloc(sizeof(node));
 50             cachehead->key=key;
 51             cachehead->value=value;
 52             cachehead->pre=NULL;
 53             cachehead->next=NULL;
 54             mp[key]=cachehead;
 55             cachetail=cachehead;
 56             count++;
 57         }
 58         else
 59         {
 60             map<int,node*>::iterator it=mp.find(key);
 61             if(it==mp.end())
 62             {
 63                 if(count==size)
 64                 {
 65                     if(cachehead==cachetail&&cachehead!=NULL)
 66                     {
 67                         mp.erase(cachehead->key);
 68                         cachehead->key=key;
 69                         cachehead->value=value;
 70                         mp[key]=cachehead;
 71                     }
 72                     else
 73                     {
 74                         node* p=cachetail;
 75                         cachetail->pre->next=cachetail->next;
 76                         cachetail=cachetail->pre;
 77                         
 78                         mp.erase(p->key);
 79                         
 80                         p->key=key;
 81                         p->value=value;
 82                         
 83                         p->next=cachehead;
 84                         p->pre=cachehead->pre;
 85                         cachehead->pre=p;
 86                         cachehead=p;
 87                         mp[cachehead->key]=cachehead;
 88                     }
 89                     
 90                 }
 91                 else
 92                 {
 93                     node* p=(node*)malloc(sizeof(node));
 94                     p->key=key;
 95                     p->value=value;
 96                     p->next=cachehead;
 97                     p->pre=cachehead->pre;
 98                     cachehead->pre=p;
 99                     cachehead=p;
100                     mp.erase(p->key);
101                     mp[cachehead->key]=cachehead;
102                     count++;
103                 }
104             }
105             else
106             {
107                 
108                 node* p=it->second;
109                 p->value=value;
110                 pushfront(p);
111              /*   mp.erase(p->key);
112                 
113                 p->pre->next=p->next;
114                 if(p->next!=NULL)
115                     p->next->pre=p->pre;
116                 p->value=value;
117                 p->next=cachehead;
118                 p->pre=cachehead->pre;
119                 cachehead->pre=p;
120                 cachehead=cachehead->pre;
121                 mp[cachehead->key]=cachehead;*/
122             }
123         }
124         
125             
126     }
127     void pushfront(node* cur)
128     {
129         if(count==1)
130             return;
131         if(cur==cachehead) return;
132         if(cur==cachetail)
133             cachetail=cur->pre;
134         cur->pre->next=cur->next;
135         if(cur->next!=NULL)
136           cur->next->pre=cur->pre;
137         cur->next=cachehead;
138         cur->pre=cachehead->pre;
139         cachehead->pre=cur;
140         cachehead=cur;
141     }
142 
143 };

 

posted @ 2015-07-24 16:39  鸭子船长  阅读(159)  评论(0编辑  收藏  举报