35657

Untitled

Jul 13th, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class List {
  7. public:
  8.  
  9.     List() : size_(0), head_(nullptr), last_(nullptr) {}
  10.  
  11.     List(const List& other) : size_(0), head_(nullptr), last_(nullptr) {
  12.         Node* temp = other.head_;
  13.         while (temp != nullptr) {
  14.             push_back(temp->value);
  15.             temp = temp->next;
  16.         }
  17.     }
  18.  
  19.     List(List&& other) : size_(other.size_), head_(other.head_), last_(other.last_) {
  20.         other.head_ = other.last_ = nullptr;
  21.     }
  22.  
  23.     List& operator=(const List& other) {
  24.         if (this != &other) {
  25.             clear();
  26.             Node* temp = other.head_;
  27.             while (temp != nullptr) {
  28.                 push_back(temp->value);
  29.                 temp = temp->next;
  30.             }
  31.         }
  32.         return *this;
  33.     }
  34.  
  35.  
  36.     List& operator=(List&& other) {
  37.         if (this != &other) {
  38.             clear();
  39.             size_ = other.size_;
  40.             head_ = other.head_;
  41.             last_ = other.last_;
  42.             other.head_ = other.last_ = nullptr;
  43.         }
  44.         return *this;
  45.     }
  46.  
  47.  
  48.     void clear() {
  49.         while (head_ != nullptr) {
  50.             pop_front();
  51.         }
  52.     }
  53.  
  54.     void push_front(const T& value) {
  55.         if (size_ == 0) {
  56.             last_ = head_ = new Node{ value, nullptr, nullptr };
  57.             size_++;
  58.             return;
  59.         }
  60.         Node* temp = new Node{ value, head_, nullptr };
  61.         head_->prev = temp;
  62.         head_ = temp;
  63.         size_++;
  64.     }
  65.  
  66.     void push_back(const T& value) {
  67.         if (size_ == 0) {
  68.             last_ = head_ = new Node{ value, nullptr, nullptr };
  69.             size_++;
  70.             return;
  71.         }
  72.         Node* temp = new Node{ value, nullptr, last_ };
  73.         last_->next = temp;
  74.         last_ = temp;
  75.         size_++;
  76.     }
  77.  
  78.     void pop_front() {
  79.         if (size_ > 0) {
  80.             if (size_ == 1) {
  81.                 delete head_;
  82.                 last_ = head_ = nullptr;
  83.                 size_--;
  84.                 return;
  85.             }
  86.             Node* temp = head_;
  87.             head_ = head_->next;
  88.             delete temp;
  89.             head_->prev = nullptr;
  90.             size_--;
  91.         }
  92.     }
  93.  
  94.     void pop_back() {
  95.         if (size_ > 0) {
  96.             if (size_ == 1) {
  97.                 delete head_;
  98.                 last_ = head_ = nullptr;
  99.                 size_--;
  100.                 return;
  101.             }
  102.             Node* temp = last_;
  103.             last_ = last_->prev;
  104.             delete temp;
  105.             last_->next = nullptr;
  106.             size_--;
  107.         }
  108.     }
  109.  
  110.     void insert(const int index, const T& value) {
  111.         if (index == 0) {
  112.             push_front(value);
  113.             return;
  114.         }
  115.         if (index == size_) {
  116.             push_back(value);
  117.             return;
  118.         }
  119.         if (index > 0 && index < size_) {
  120.             Node* temp = head_;
  121.             for (int i = 0; i < index - 1; i++) {
  122.                 temp = temp->next;
  123.             }
  124.             Node* buf = new Node{ value, temp->next, temp };
  125.             temp->next->prev = buf;
  126.             temp->next = buf;
  127.             size_++;
  128.         }
  129.     }
  130.  
  131.     void erase(const int index) {
  132.         if (index == 0) {
  133.             pop_front();
  134.             return;
  135.         }
  136.         if (index == size_ - 1) {
  137.             pop_back();
  138.             return;
  139.         }
  140.         if (index > 0 && index < size_ - 1) {
  141.             Node* temp = head_;
  142.             for (int i = 0; i < index - 1; i++) {
  143.                 temp = temp->next;
  144.             }
  145.             Node* buf = temp->next->next;
  146.             delete temp->next;
  147.             temp->next = buf;
  148.             buf->prev = temp;
  149.             size_--;
  150.         }
  151.     }
  152.  
  153.     T& front() {
  154.         if (head_ != nullptr) {
  155.             return head_->value;
  156.         }
  157.     }
  158.  
  159.     const T& front() const {
  160.         if (head_ != nullptr) {
  161.             return head_->value;
  162.         }
  163.     }
  164.  
  165.     T& back() {
  166.         if (last_ != nullptr) {
  167.             return last_->value;
  168.         }
  169.     }
  170.  
  171.     const T& back() const {
  172.         if (last_ != nullptr) {
  173.             return last_->value;
  174.         }
  175.     }
  176.  
  177.     void print() {
  178.         Node* temp = head_;
  179.         while (temp != nullptr) {
  180.             cout << temp->value << " ";
  181.             temp = temp->next;
  182.         }
  183.         cout << endl;
  184.     }
  185.  
  186.     int size() const {
  187.         return size_;
  188.     }
  189.  
  190.     T& operator[] (const int index) {
  191.         if (index >= 0 && index < size_) {
  192.             Node* temp = head_;
  193.             for (int i = 0; i < index; i++) {
  194.                 temp = temp->next;
  195.             }
  196.             return temp->value;
  197.         }
  198.     }
  199.  
  200.     const T& operator[] (const int index) const {
  201.         if (index >= 0 && index < size_) {
  202.             Node* temp = head_;
  203.             for (int i = 0; i < index; i++) {
  204.                 temp = temp->next;
  205.             }
  206.             return temp->value;
  207.         }
  208.     }
  209.  
  210.     bool operator==(const List& other) const {
  211.         if (size_ != other.size_) {
  212.             return false;
  213.         }
  214.         Node* temp = head_;
  215.         Node* other_temp = other.head_;
  216.  
  217.         while (temp != nullptr) {
  218.             if (temp->value != other_temp->value) {
  219.                 return false;
  220.             }
  221.             temp = temp->next;
  222.             other_temp = other_temp->next;
  223.         }
  224.         return true;
  225.     }
  226.  
  227.     bool operator!=(const List& other) const {
  228.         return !(*this == other);
  229.     }
  230.  
  231.     int find(const T& value) const {
  232.         int index = 0;
  233.         Node* temp = head_;
  234.  
  235.         while (temp != nullptr && temp->value != value) {
  236.             temp = temp->next;
  237.             index++;
  238.         }
  239.         if (temp != nullptr) {
  240.             return index;
  241.         }
  242.         return -1;
  243.     }
  244.  
  245.     ~List() {
  246.         clear();
  247.     }
  248.  
  249. private:
  250.     struct Node { // двусвязный список состоит из узлов
  251.         T value; // узел хранит информативную часть
  252.         Node* next; // указатель на следующий узел в списке
  253.         Node* prev; // указатель на предыдущий узел
  254.     };
  255.  
  256.     int size_ = 0;
  257.     Node* head_ = nullptr;
  258.     Node* last_ = nullptr;
  259. };
  260.  
  261. int main() {
  262.     List<int> list1;
  263.     for (int i = 0; i < 10; i++) {
  264.         list1.push_front(i + 1);
  265.     }
  266.     list1.print();
  267.     cout << list1.size() << endl;
  268.  
  269.     for (int i = 0; i < 3; i++) {
  270.         list1.pop_front();
  271.     }
  272.     list1.print();
  273.     cout << list1.size() << endl;
  274.  
  275.     List<int> list2(list1);
  276.     list2.print();
  277.     cout << list2.size() << endl;
  278.  
  279.     List<int> list3;
  280.     list3 = move(list1);
  281.     list3.print();
  282.  
  283.     list3.insert(2, 34);
  284.     list3.insert(5, 35);
  285.     list3.print();
  286.     list3.erase(4);
  287.     list3.print();
  288.     cout << list3[4] << endl;
  289.     cout << list3.back() << endl;
  290.     cout << list3.front() << endl;
  291.     cout << (list2 == list3) << endl;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment