KeiroKamioka

WHHHYYYYY!!!!!

Mar 16th, 2021
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 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.         node* curr = this;
  56.         while (curr->next != nullptr) {
  57.             curr = curr->next;
  58.         }
  59.  
  60.         curr->next = new node(value1, value2);
  61.  
  62.         //Your code ends here
  63.     }
  64.     //Find the value2 corresponding to the value1 passed as parameter
  65.     T find_value1(U value2)
  66.     {
  67.         //Your code starts here
  68.         node* curr = this;
  69.         node* r;
  70.         while (curr != nullptr)
  71.         {
  72.             if (curr->next == nullptr) {
  73.                 r = curr;
  74.             }
  75.             if (curr->value->get_value2() == value2) {
  76.                 return curr->value->get_value1();
  77.  
  78.             }
  79.             curr = curr->next;
  80.         }
  81.         return r->value->get_value1();
  82.  
  83.  
  84.         //Your code ends here
  85.     }
  86.     //Find the value1 corresponding to the value2 passed as parameter
  87.     U find_value2(T value1)
  88.     {
  89.         //Your code starts here
  90.         node* curr = this;
  91.         node* r;
  92.         while (curr != nullptr)
  93.         {
  94.             if (curr->next == nullptr) {
  95.                 r = curr;
  96.             }
  97.             if (curr->value->get_value1() == value1) {
  98.                 return curr->value->get_value2();
  99.             }
  100.             curr = curr->next;
  101.         }
  102.         return r->value->get_value2();
  103.  
  104.         //Your code ends here
  105.     }
  106.  
  107.     void print()
  108.     {
  109.         node* curr = this;
  110.         while (curr != nullptr)
  111.         {
  112.             cout << curr->value->get_value1() << "->" << curr->value->get_value2() << endl;
  113.             curr = curr->next;
  114.         }
  115.     }
  116. };
  117. int main() {
  118.  
  119.     node<int, string>* test = new node<int, string>(1, "one");
  120.     test->add(2, "2");
  121.     test->add(3, "three");
  122.     test->print();
  123.  
  124.     node<string, string>* test2 = new node<string, string>("itichi", "one");
  125.     test2->add("Ni", "two");
  126.     test2->add("San", "three");
  127.     test2->print();
  128.  
  129.    
  130.  
  131.  
  132.  
  133.     return 0;
  134. }
  135.  
  136.  
Advertisement
Add Comment
Please, Sign In to add comment