方法一:双链表+哈希表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;
}