fork download
  1. class Node{
  2. public:
  3. int key;
  4. int val;
  5. Node* prev;
  6. Node* next;
  7.  
  8. Node(int key,int val){
  9. this->key = key;
  10. this->val = val;
  11. prev = NULL;
  12. next = NULL;
  13. }
  14. };
  15.  
  16. class LRUCache {
  17. public:
  18.  
  19. Node* head = new Node(-1,-1);
  20. Node* tail = new Node(-1,-1);
  21. int capacity;
  22. unordered_map<int,Node*> m;
  23.  
  24. LRUCache(int _capacity) {
  25. capacity = _capacity;
  26. head->next = tail;
  27. tail->prev = head;
  28. }
  29. void addNode(Node* New){
  30. Node* temp = head->next;
  31. New->next = temp;
  32. New->prev = head;
  33. head->next = New;
  34. temp->prev = New;
  35. }
  36. void deleteNode(Node* del){
  37. Node* delprev = del->prev;
  38. Node* delnext = del->next;
  39. delprev->next = delnext;
  40. delnext->prev = delprev;
  41. }
  42.  
  43.  
  44.  
  45. int get(int key) {
  46. if(m.find(key)!=m.end()){
  47. Node* resultNode = m[key];
  48. int ans = resultNode->val;
  49. m.erase(key);
  50. deleteNode(resultNode);
  51. addNode(resultNode);
  52. m[key] = head->next;
  53. return ans;
  54. }
  55. else return -1;
  56. }
  57.  
  58. void put(int key, int value) {
  59. if(m.find(key)!=m.end()){
  60. Node* delNode = m[key];
  61. m.erase(key);
  62. deleteNode(delNode);
  63. }
  64. if(m.size()==capacity){
  65. m.erase(tail->prev->key);
  66. deleteNode(tail->prev);
  67. }
  68. Node* New = new Node(key,value);
  69. addNode(New);
  70. m[key] = head->next;
  71. }
  72. };
  73.  
  74. /**
  75.  * Your LRUCache object will be instantiated and called as such:
  76.  * LRUCache* obj = new LRUCache(capacity);
  77.  * int param_1 = obj->get(key);
  78.  * obj->put(key,value);
  79.  */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor ‘Node::Node(int, int)’:
prog.cpp:11:20: error: ‘NULL’ was not declared in this scope
             prev = NULL;
                    ^~~~
prog.cpp:11:20: note: ‘NULL’ is defined in header ‘<cstddef>’; did you forget to ‘#include <cstddef>’?
prog.cpp:1:1:
+#include <cstddef>
 class Node{
prog.cpp:11:20:
             prev = NULL;
                    ^~~~
prog.cpp: At global scope:
prog.cpp:22:5: error: ‘unordered_map’ does not name a type
     unordered_map<int,Node*> m;
     ^~~~~~~~~~~~~
prog.cpp: In member function ‘int LRUCache::get(int)’:
prog.cpp:46:12: error: ‘m’ was not declared in this scope
         if(m.find(key)!=m.end()){
            ^
prog.cpp: In member function ‘void LRUCache::put(int, int)’:
prog.cpp:59:11: error: ‘m’ was not declared in this scope
        if(m.find(key)!=m.end()){
           ^
prog.cpp:64:12: error: ‘m’ was not declared in this scope
         if(m.size()==capacity){
            ^
prog.cpp:70:9: error: ‘m’ was not declared in this scope
         m[key] = head->next;
         ^
stdout
Standard output is empty