Advertisement
KeiroKamioka

Now

Mar 16th, 2021
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //The class my_pair which stores two values of different types referred to as value1 and value2
  5. //Your code starts here
  6. template <class T, class U>
  7. //Your code ends here
  8. class my_pair
  9. {
  10.     //Your code starts here
  11.     T value1;
  12.     U value2;
  13.     //Your code ends here
  14.  
  15. public:
  16.     //Your code starts here
  17.     my_pair(T x, U y) {
  18.         value1 = x;
  19.         value2 = y;
  20.  
  21.     }
  22.  
  23.     //Your code ends here
  24.  
  25.     T get_value1()
  26.     {
  27.         return this->value1;
  28.     }
  29.     U get_value2()
  30.     {
  31.         return this->value2;
  32.     }
  33. };
  34.  
  35. //The class node implements a linked list of pairs
  36. template <class T, class U>
  37. class node
  38. {
  39.     my_pair<T, U>* value;
  40.     node* next;
  41.  
  42. public:
  43.     //Creates a new node
  44.     node(T value1, U value2)
  45.     {
  46.         //Your code starts here
  47.         this->value = new my_pair<T, U>(value1, value2);
  48.  
  49.         //Your code ends here
  50.     }
  51.     //Add a new pair at the end of the list
  52.     void add(T value1, U value2)
  53.     {
  54.         //Your code starts here
  55.         this->next = new node(value1, value2);
  56.         //Your code ends here
  57.     }
  58.     //Find the value2 corresponding to the value1 passed as parameter
  59.     T find_value1(U value2)
  60.     {
  61.         //Your code starts here
  62.         node* curr = this;
  63.         node* r;
  64.         while (curr != nullptr)
  65.         {
  66.             if (curr->next == nullptr)
  67.                 r = curr;
  68.             if (curr->value->get_value2() == value2)
  69.                 return curr->value->get_value1();
  70.             curr = curr->next;
  71.         }
  72.         return r->value->get_value1();
  73.  
  74.  
  75.         //Your code ends here
  76.     }
  77.     //Find the value1 corresponding to the value2 passed as parameter
  78.     U find_value2(T value1)
  79.     {
  80.         //Your code starts here
  81.         node* curr = this;
  82.         node* r;
  83.         while (curr != nullptr)
  84.         {
  85.             if (curr->next == nullptr)
  86.                 r = curr;
  87.             if (curr->value->get_value1() == value1)
  88.                 return curr->value->get_value2();
  89.             curr = curr->next;
  90.         }
  91.         return r->value->get_value2();
  92.  
  93.         //Your code ends here
  94.     }
  95.  
  96.     void print()
  97.     {
  98.         node* curr = this;
  99.         while (curr != nullptr)
  100.         {
  101.             cout << curr->value->get_value1() << "->" << curr->value->get_value2() << endl;
  102.             curr = curr->next;
  103.         }
  104.     }
  105. };
  106. int main() {
  107.  
  108.     node<int, string>* test = new node<int, string>(1, "one");
  109.     test->add(2, "2");
  110.     test->add(3, "three");
  111.     test->print();
  112.  
  113.  
  114.    
  115.  
  116.  
  117.  
  118.     return 0;
  119. }
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement