Advertisement
35657

Untitled

Apr 16th, 2024
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.82 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class Vector {
  7.  
  8. public:
  9.     Vector() : size_(0), capacity_(4), arr_(new T[4]) {}
  10.  
  11.     explicit Vector(const int vector_capacity) : size_(0), capacity_(vector_capacity), arr_(new T[vector_capacity]) {}
  12.  
  13.     Vector(const Vector& other) : size_(other.size_), capacity_(other.capacity_), arr_(new T[capacity_]) {
  14.         for (int i = 0; i < size_; i++) {
  15.             arr_[i] = other.arr_[i];
  16.         }
  17.         total_number_elements_ += size_;
  18.     } // конструктор копирования
  19.  
  20.     Vector(const initializer_list<T>& list) : size_(list.size()), capacity_(list.size()), arr_(new T[list.size()]) {
  21.         int i = 0;
  22.         for (const T& element : list) {
  23.             arr_[i] = element;
  24.             i++;
  25.         }
  26.     }
  27.  
  28.     Vector& operator=(const Vector& other) {
  29.         if (&other != this) { // проверка на самоприсваивание
  30.             total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
  31.             size_ = other.size_;
  32.             capacity_ = other.capacity_;
  33.             delete[] arr_;
  34.             arr_ = new T[capacity_];
  35.             for (int i = 0; i < size_; i++) {
  36.                 arr_[i] = other.arr_[i];
  37.             }
  38.         }
  39.         return *this;
  40.     } // копирующий оператор присваивания =
  41.  
  42.     Vector(Vector&& other) : size_(other.size_), capacity_(other.capacity_), arr_(other.arr_) {
  43.         other.arr_ = nullptr;
  44.         other.size_ = 0;
  45.         other.capacity_ = 0;
  46.     } // конструктор перемещения
  47.  
  48.     Vector& operator=(Vector&& other) {
  49.         if (&other != this) {
  50.             total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
  51.             size_ = other.size_;
  52.             capacity_ = other.capacity_;
  53.             delete[] arr_;
  54.             arr_ = other.arr_;
  55.             other.arr_ = nullptr;
  56.             other.size_ = 0;
  57.             other.capacity_ = 0;
  58.         }
  59.         return *this;
  60.     } // перемещающий оператор присваивания =
  61.  
  62.     void push_back(const T& value) {
  63.         check_capacity();
  64.         arr_[size_] = value;
  65.         size_++;
  66.         total_number_elements_++;
  67.     }
  68.  
  69.     void pop_back() {
  70.         if (size_ > 0) {
  71.             size_--;
  72.         }
  73.         total_number_elements_--;
  74.     }
  75.  
  76.     void insert(const int index, const T& value) {
  77.         if (index < 0 || index > size_) {
  78.             cout << "Некорректный индекс" << endl;
  79.             return;
  80.         }
  81.         check_capacity();
  82.         for (int i = size_; i > index; i--) {
  83.             arr_[i] = arr_[i - 1];
  84.         }
  85.         arr_[index] = value;
  86.         size_++;
  87.         total_number_elements_++;
  88.     }
  89.  
  90.     void erase(const int index) {
  91.         if (index < 0 || index >= size_) {
  92.             cout << "Некорректный индекс" << endl;
  93.             return;
  94.         }
  95.         for (int i = index; i < size_ - 1; i++) {
  96.             arr_[i] = arr_[i + 1];
  97.         }
  98.         size_--;
  99.         total_number_elements_--;
  100.     }
  101.  
  102.     T& operator[](const int index) {
  103.         if (index < 0 || index >= size_) {
  104.             cout << "Некорректный индекс" << endl;
  105.         }
  106.         return arr_[index];
  107.     }
  108.  
  109.     const T& operator[](const int index) const {
  110.         if (index < 0 || index >= size_) {
  111.             cout << "Некорректный индекс" << endl;
  112.         }
  113.         return arr_[index];
  114.     }
  115.  
  116.     int size() const {
  117.         return size_;
  118.     }
  119.  
  120.     int capacity() const {
  121.         return capacity_;
  122.     }
  123.  
  124.     bool operator==(const Vector& right) const {
  125.         if (right.size_ != size_) {
  126.             return false;
  127.         }
  128.         for (int i = 0; i < size_; i++) {
  129.             if (arr_[i] != right.arr_[i])
  130.                 return false;
  131.         }
  132.         return true;
  133.     }
  134.  
  135.     bool operator!=(const Vector& right) const {
  136.         return !(*this == right);
  137.     }
  138.  
  139.     void print() const {
  140.         for (int i = 0; i < size_; i++) {
  141.             cout << arr_[i] << " ";
  142.         }
  143.         cout << endl;
  144.     }
  145.  
  146.     static int get_total_number_elements() {
  147.         return total_number_elements_;
  148.     }
  149.  
  150.  
  151.     ~Vector() {
  152.         delete[] arr_;
  153.         total_number_elements_ -= size_;
  154.     }
  155.  
  156. private:
  157.  
  158.     void check_capacity() {
  159.         if (size_ == capacity_) {
  160.             int* temp = new int[capacity_ * 2];
  161.             for (int i = 0; i < size_; i++) {
  162.                 temp[i] = arr_[i];
  163.             }
  164.             delete[] arr_;
  165.             arr_ = temp;
  166.             capacity_ *= 2;
  167.         }
  168.     }
  169.  
  170.     int size_; // текущее количество элементов
  171.     int capacity_; // емкость хранилища
  172.     T* arr_; // хранилище
  173.     static int total_number_elements_; // общее количество элементов по всем векторам
  174. };
  175.  
  176. template <typename T>
  177. int Vector<T>::total_number_elements_ = 0;
  178.  
  179. template <typename T>
  180. class List {
  181. public:
  182.  
  183.     List() : size_(0), head_(nullptr), last_(nullptr) {}
  184.  
  185.     List(const List& other) : size_(0), head_(nullptr), last_(nullptr) {
  186.         Node* temp = other.last_;
  187.         while (temp != nullptr) {
  188.             push_front(temp->value);
  189.             temp = temp->prev;
  190.         }
  191.     }
  192.  
  193.     List(List&& other) : size_(other.size_), head_(other.head_), last_(other.last_) {
  194.         other.head_ = other.last_ = nullptr;
  195.     }
  196.  
  197.     List& operator=(const List& other) {
  198.         if (this != &other) {
  199.             clear();
  200.             Node* temp = other.last_;
  201.             while (temp != nullptr) {
  202.                 push_front(temp->value);
  203.                 temp = temp->prev;
  204.             }
  205.         }
  206.         return *this;
  207.     }
  208.  
  209.     List& operator=(List&& other) {
  210.         if (this != &other) {
  211.             clear();
  212.             size_ = other.size_;
  213.             head_ = other.head_;
  214.             last_ = other.last_;
  215.             other.head_ = other.last_ = nullptr;
  216.         }
  217.         return *this;
  218.     }
  219.  
  220.     void push_front(const T& value) {
  221.         if (size_ == 0) {
  222.             last_ = head_ = new Node{ value, nullptr, nullptr };
  223.             size_++;
  224.             return;
  225.         }
  226.         Node* temp = new Node{ value, head_, nullptr };
  227.         head_->prev = temp;
  228.         head_ = temp;
  229.         size_++;
  230.     }
  231.  
  232.     void push_back(const T& value) {
  233.         if (size_ == 0) {
  234.             last_ = head_ = new Node{ value, nullptr, nullptr };
  235.             size_++;
  236.             return;
  237.         }
  238.         Node* temp = new Node{ value, nullptr, last_ };
  239.         last_->next = temp;
  240.         last_ = temp;
  241.         size_++;
  242.     }
  243.  
  244.     void pop_front() {
  245.         if (size_ > 0) {
  246.             if (size_ == 1) {
  247.                 delete head_;
  248.                 last_ = head_ = nullptr;
  249.                 size_--;
  250.                 return;
  251.             }
  252.             Node* temp = head_;
  253.             head_ = head_->next;
  254.             delete temp;
  255.             head_->prev = nullptr;
  256.             size_--;
  257.         }
  258.     }
  259.  
  260.     void pop_back() {
  261.         if (size_ > 0) {
  262.             if (size_ == 1) {
  263.                 delete head_;
  264.                 last_ = head_ = nullptr;
  265.                 size_--;
  266.                 return;
  267.             }
  268.             Node* temp = last_;
  269.             last_ = last_->prev;
  270.             delete temp;
  271.             last_->next = nullptr;
  272.             size_--;
  273.         }
  274.     }
  275.  
  276.     void insert(const int index, const T& value) {
  277.         if (index == 0) {
  278.             push_front(value);
  279.             return;
  280.         }
  281.         if (index == size_) {
  282.             push_back(value);
  283.             return;
  284.         }
  285.         if (index > 0 && index < size_) {
  286.             Node* temp = head_;
  287.             for (int i = 0; i < index - 1; i++) {
  288.                 temp = temp->next;
  289.             }
  290.             Node* buf = new Node{ value, temp->next, temp };
  291.             temp->next->prev = buf;
  292.             temp->next = buf;
  293.             size_++;
  294.         }
  295.     }
  296.  
  297.     void erase(const int index) {
  298.         if (index == 0) {
  299.             pop_front();
  300.             return;
  301.         }
  302.         if (index == size_ - 1) {
  303.             pop_back();
  304.             return;
  305.         }
  306.         if (index > 0 && index < size_ - 1) {
  307.             Node* temp = head_;
  308.             for (int i = 0; i < index - 1; i++) {
  309.                 temp = temp->next;
  310.             }
  311.             Node* buf = temp->next->next;
  312.             delete temp->next;
  313.             temp->next = buf;
  314.             buf->prev = temp;
  315.             size_--;
  316.         }
  317.     }
  318.  
  319.     T& operator[] (const int index) {
  320.         if (index >= 0 && index < size_) {
  321.             Node* temp = head_;
  322.             for (int i = 0; i < index; i++) {
  323.                 temp = temp->next;
  324.             }
  325.             return temp->value;
  326.         }
  327.     }
  328.  
  329.     const T& operator[] (const int index) const {
  330.         if (index >= 0 && index < size_) {
  331.             Node* temp = head_;
  332.             for (int i = 0; i < index; i++) {
  333.                 temp = temp->next;
  334.             }
  335.             return temp->value;
  336.         }
  337.     }
  338.  
  339.     bool operator==(const List& other) const {
  340.         if (size_ != other.size_) {
  341.             return false;
  342.         }
  343.         Node* temp = head_;
  344.         Node* other_temp = other.head_;
  345.  
  346.         while (temp != nullptr) {
  347.             if (temp->value != other_temp->value) {
  348.                 return false;
  349.             }
  350.             temp = temp->next;
  351.             other_temp = other_temp->next;
  352.         }
  353.         return true;
  354.     }
  355.  
  356.     bool operator!=(const List& other) const {
  357.         return !(*this == other);
  358.     }
  359.  
  360.     int find(const T& value) const {
  361.         int index = 0;
  362.         Node* temp = head_;
  363.  
  364.         while (temp != nullptr && temp->value != value) {
  365.             temp = temp->next;
  366.             index++;
  367.         }
  368.         if (temp != nullptr) {
  369.             return index;
  370.         }
  371.         return -1;
  372.     }
  373.  
  374.     T& front() {
  375.         if (head_ != nullptr) {
  376.             return head_->value;
  377.         }
  378.     }
  379.  
  380.     const T& front() const {
  381.         if (head_ != nullptr) {
  382.             return head_->value;
  383.         }
  384.     }
  385.  
  386.     T& back() {
  387.         if (last_ != nullptr) {
  388.             return last_->value;
  389.         }
  390.     }
  391.  
  392.     const T& back() const {
  393.         if (last_ != nullptr) {
  394.             return last_->value;
  395.         }
  396.     }
  397.  
  398.     void print() {
  399.         Node* temp = head_;
  400.         while (temp != nullptr) {
  401.             cout << temp->value << " ";
  402.             temp = temp->next;
  403.         }
  404.         cout << endl;
  405.     }
  406.  
  407.     int size() const {
  408.         return size_;
  409.     }
  410.  
  411.     void clear() {
  412.         while (head_ != nullptr) {
  413.             pop_front();
  414.         }
  415.     }
  416.  
  417.     ~List() {
  418.         clear();
  419.     }
  420.  
  421. private:
  422.     struct Node { // двусвязный список состоит из узлов
  423.         T value; // узел хранит информативную часть
  424.         Node* next; // указатель на следующий узел в списке
  425.         Node* prev; // указатель на предыдущий узел
  426.     };
  427.  
  428.     int size_;
  429.     Node* head_;
  430.     Node* last_;
  431. };
  432.  
  433. template<typename T>
  434. class Set {
  435.  
  436. public:
  437.  
  438.     struct Node {
  439.         T value;
  440.         Node* left;
  441.         Node* right;
  442.         Node* parent;
  443.     };
  444.  
  445.     Set() : root_(nullptr), size_(0) {}
  446.  
  447.     Set(const initializer_list<T>& list) : root_(nullptr), size_(0) {
  448.         for (const T& element : list) {
  449.             insert(element);
  450.         }
  451.     }
  452.  
  453.     Set(const Set& other) : root_(copy(other.root_, nullptr)), size_(other.size_) {}
  454.  
  455.     Set(Set&& other) : root_(other.root_), size_(other.size_) {
  456.         other.root_ = nullptr;
  457.     }
  458.  
  459.     Set& operator=(const Set& other) {
  460.         if (this != &other) {
  461.             clear();
  462.             size_ = other.size_;
  463.             root_ = copy(other.root_, nullptr);
  464.         }
  465.         return *this;
  466.     }
  467.  
  468.     Set& operator=(Set&& other) {
  469.         if (this != &other) {
  470.             clear();
  471.             size_ = other.size_;
  472.             root_ = other.root_;
  473.             other.root_ = nullptr;
  474.         }
  475.         return *this;
  476.     }
  477.  
  478.     void insert(const T& val) {
  479.         if (root_ == nullptr) {
  480.             root_ = new Node{ val, nullptr, nullptr, nullptr };
  481.             size_++;
  482.             return;
  483.         }
  484.         Node* parent = nullptr;
  485.         Node* node = root_;
  486.         while (node != nullptr && node->value != val) {
  487.             parent = node;
  488.             node = node->value > val ? node->left : node->right;
  489.         }
  490.         if (node == nullptr) {
  491.             node = new Node{ val, nullptr, nullptr, parent };
  492.             parent->value > val ? parent->left = node : parent->right = node;
  493.             size_++;
  494.         }
  495.     }
  496.  
  497.     void erase(const T& val) {
  498.         Node* parent = nullptr;
  499.         Node* node = root_;
  500.         while (node != nullptr && node->value != val) {
  501.             parent = node;
  502.             node = node->value > val ? node->left : node->right;
  503.         }
  504.         if (node != nullptr) {
  505.             if (node->left == nullptr && node->right == nullptr) {
  506.                 if (node == root_) {
  507.                     root_ = nullptr;
  508.                 }
  509.                 else {
  510.                     node == parent->right ? parent->right = nullptr : parent->left = nullptr;
  511.                 }
  512.                 delete node;
  513.             }
  514.             else if (node->left == nullptr) {
  515.                 node->right->parent = node->parent;
  516.                 if (node == root_) {
  517.                     root_ = node->right;
  518.                 }
  519.                 else {
  520.                     node == parent->right ? parent->right = node->right : parent->left = node->right;
  521.                 }
  522.                 delete node;
  523.             }
  524.             else if (node->right == nullptr) {
  525.                 node->left->parent = node->parent;
  526.                 if (node == root_) {
  527.                     root_ = node->left;
  528.                 }
  529.                 else {
  530.                     node == parent->right ? parent->right = node->left : parent->left = node->left;
  531.                 }
  532.                 delete node;
  533.             }
  534.             else {
  535.                 Node* temp = min(node->right);
  536.                 node->value = temp->value;
  537.                 if (temp->right != nullptr) {
  538.                     temp->right->parent = temp->parent;
  539.                 }
  540.                 temp == temp->parent->right ? temp->parent->right = temp->right : temp->parent->left = temp->right;
  541.                 delete temp;
  542.             }
  543.             size_--;
  544.         }
  545.     }
  546.  
  547.     Node* min(Node* node) {
  548.         if (node != nullptr) {
  549.             while (node->left != nullptr) {
  550.                 node = node->left;
  551.             }
  552.         }
  553.         return node;
  554.     }
  555.  
  556.     Node* max(Node* node) {
  557.         if (node != nullptr) {
  558.             while (node->right != nullptr) {
  559.                 node = node->right;
  560.             }
  561.         }
  562.         return node;
  563.     }
  564.  
  565.     void print() {
  566.         print(root_);
  567.         cout << endl;
  568.     }
  569.  
  570.     int size() const {
  571.         return size_;
  572.     }
  573.  
  574.     void clear() {
  575.         clear(root_);
  576.         root_ = nullptr;
  577.         size_ = 0;
  578.     }
  579.  
  580.     Node* find(const T& val) {
  581.         Node* node = root_;
  582.         while (node != nullptr && node->value != val) {
  583.             node = node->value > val ? node->left : node->right;
  584.         }
  585.         return node;
  586.     }
  587.  
  588.     Node* begin() {
  589.         return min(root_);
  590.     }
  591.  
  592.     Node* end() {
  593.         return max(root_);
  594.     }
  595.  
  596.     bool operator==(const Set& other) {
  597.         return size_ == other.size_ ? compare(min(root_), min(other.root_)) : false;
  598.     }
  599.  
  600.     bool operator!=(const Set& other) {
  601.         return !(*this == other);
  602.     }
  603.  
  604.     ~Set() {
  605.         clear(root_);
  606.     }
  607.  
  608. private:
  609.  
  610.     // вариант сравнения деревьев на случай разной топологии но одинакового размера
  611.     bool compare(Node* a, Node* b) {
  612.         if (a == nullptr && b == nullptr) {
  613.             return true;
  614.         }
  615.         return  a->value == b->value && compare(next(a), next(b));
  616.     }
  617.  
  618.     Node* next(Node* node) {
  619.         if (node != nullptr) {
  620.             if (node->right != nullptr) {
  621.                 return min(node->right);
  622.             }
  623.             Node* temp = node->parent;
  624.             while (temp != nullptr && node->value > temp->value) {
  625.                 temp = temp->parent;
  626.             }
  627.             return temp;
  628.         }
  629.         return node;
  630.     }
  631.  
  632.     Node* prev(Node* node) {
  633.         if (node != nullptr) {
  634.             if (node->left != nullptr) {
  635.                 return max(node->left);
  636.             }
  637.             Node* temp = node->parent;
  638.             while (temp != nullptr && node->value < temp->value) {
  639.                 temp = temp->parent;
  640.             }
  641.             return temp;
  642.         }
  643.         return node;
  644.     }
  645.  
  646.     Node* copy(Node* node, Node* parent) {
  647.         if (node == nullptr) {
  648.             return nullptr;
  649.         }
  650.         Node* temp = new Node{ node->value, nullptr, nullptr, parent };
  651.         temp->left = copy(node->left, temp);
  652.         temp->right = copy(node->right, temp);
  653.         return temp;
  654.     }
  655.  
  656.     void clear(Node* node) {
  657.         if (node != nullptr) {
  658.             clear(node->left);
  659.             clear(node->right);
  660.             delete node;
  661.         }
  662.     }
  663.  
  664.     void print(Node* node) {
  665.         if (node != nullptr) {
  666.             print(node->left);
  667.             cout << node->value << " ";
  668.             // раскомментировать для проверки построения дерева
  669.            /* cout << " value " << node->value << " ";
  670.             if (node->parent) {
  671.                 cout << " parent " << node->parent->value;
  672.             }
  673.             else {
  674.                 cout << '\t';
  675.             }
  676.             if (node->left) {
  677.                 cout << " left " << node->left->value;
  678.             }
  679.             else {
  680.                 cout << '\t';
  681.             }
  682.             if (node->right) {
  683.                 cout << " right " << node->right->value;
  684.             }
  685.             else {
  686.                 cout << '\t';
  687.             }
  688.             cout << " " << node << endl;*/
  689.             print(node->right);
  690.         }
  691.     }
  692.  
  693.     Node* root_;
  694.     int size_;
  695. };
  696.  
  697. int main() {
  698.     setlocale(LC_ALL, "ru");
  699.  
  700.     // проверить функционал каждого класса
  701.    
  702. }
  703.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement