You are viewing a single comment's thread. Return to all comments →
class LRUCache:public Cache { int cap; public: LRUCache(int capacity) { cp=capacity; cap=capacity; head=nullptr; tail=nullptr; } void set(int key,int value) { if(cap==0) { return; }
if(tail==nullptr && head==nullptr) { Node *firstNode=new Node(key,value); tail=firstNode; head=firstNode; mp.insert({key,firstNode}); cp--; } else if(cp==0 && mp.find(key)==mp.end())//delete last add to first new node { Node *newNode=new Node(nullptr,head,key,value); head->prev=newNode; head=newNode; mp.erase(tail->key); Node *temp=tail; tail=tail->prev; tail->next=nullptr; free(temp); } else if(cp>0 && mp.find(key)==mp.end()) { //modified add to first Node *nextnode=new Node(nullptr,head,key,value); head->prev=nextnode;//existing head previous head=nextnode; mp.insert({key,nextnode}); cp--; } else { mp[key]->value=value; } } int get(int key) { if(mp.find(key)!=mp.end()) { return mp[key]->value; } else { return -1; } }
};
Seems like cookies are disabled on this browser, please enable them to open this website
Abstract Classes - Polymorphism
You are viewing a single comment's thread. Return to all comments →
class LRUCache:public Cache { int cap; public: LRUCache(int capacity) { cp=capacity; cap=capacity; head=nullptr; tail=nullptr; } void set(int key,int value) { if(cap==0) { return; }
};