Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //The class my_pair which stores two values of different types referred to as value1 and value2
- //Your code starts here
- template <class T, class U>
- //Your code ends here
- class my_pair
- {
- //Your code starts here
- T value1;
- U value2;
- //Your code ends here
- public:
- //Your code starts here
- my_pair(T x, U y) {
- value1 = x;
- value2 = y;
- }
- //Your code ends here
- T get_value1()
- {
- return this->value1;
- }
- U get_value2()
- {
- return this->value2;
- }
- };
- //The class node implements a linked list of pairs
- template <class T, class U>
- class node
- {
- my_pair<T, U>* value;
- node* next;
- public:
- //Creates a new node
- node(T value1, U value2)
- {
- //Your code starts here
- this->value = new my_pair<T, U>(value1, value2);
- //Your code ends here
- }
- //Add a new pair at the end of the list
- void add(T value1, U value2)
- {
- //Your code starts here
- this->next = new node(value1, value2);
- //Your code ends here
- }
- //Find the value2 corresponding to the value1 passed as parameter
- T find_value1(U value2)
- {
- //Your code starts here
- node* curr = this;
- while (curr != nullptr)
- {
- if (curr->value2 == value2)
- return curr->value1;
- curr = curr->next;
- }
- //Your code ends here
- }
- //Find the value1 corresponding to the value2 passed as parameter
- U find_value2(T value1)
- {
- //Your code starts here
- node* curr = this;
- while (curr != nullptr)
- {
- if (curr->value1 == value1)
- return curr->value2;
- curr = curr->next;
- }
- //Your code ends here
- }
- void print()
- {
- node* curr = this;
- while (curr != nullptr)
- {
- cout << curr->value->get_value1() << "->" << curr->value->get_value2() << endl;
- curr = curr->next;
- }
- }
- };
- int main() {
- node<int, string>* test = new node<int, string> (1, "one");
- test->add(2, "2");
- test->add(3, "three");
- test->print();
- test->find_value1("2");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement