Guest User

Untitled

a guest
May 28th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. typedef int World;
  2. class Dictionary {
  3.     struct Element {
  4.         int m_addr;
  5.         World m_value;
  6.         Element* m_next;
  7.     };
  8.     Element* a_head;
  9.  
  10.     Element* find(int a_addr)
  11.     {
  12.         Element* current = a_head;
  13.         while( current ) {
  14.             if( current->m_addr == a_addr )
  15.                 return current;
  16.             current = current->m_next;
  17.         }
  18.         return 0;
  19.     }
  20.    
  21.     Element* add(int a_addr)
  22.     {
  23.         assert( !find(a_addr) );
  24.         Element* new_el = new Element;
  25.         new_el->m_addr = a_addr;
  26.         new_al->m_next = a_head;
  27.         a_head = new_al;
  28.     }
  29. public:
  30.     Dictionary() : a_head() {}
  31.     ~Dictionary()
  32.     {
  33.         Element* current = a_head;
  34.         while( current ) {
  35.             Element* for_del = current;
  36.             current = for_del->m_next;
  37.             delete current;
  38.         }
  39.     }
  40.    
  41.     void save(int a_addr, World a_value)
  42.     {
  43.         Element* e = find(a_addr);
  44.         if( !a )
  45.             e = add(a_addr);
  46.         e->m_value = a_value;
  47.     }
  48.  
  49.     void load(int a_addr)
  50.     {
  51.         Element* e = find(a_addr);
  52.         assert(e);
  53.         return e->m_value;
  54.     }
  55. };
Add Comment
Please, Sign In to add comment