Advertisement
35657

Untitled

Mar 23rd, 2024 (edited)
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  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.last_;
  13.         while (temp != nullptr) {
  14.             push_front(temp->value);
  15.             temp = temp->prev;
  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.last_;
  27.             while (temp != nullptr) {
  28.                 push_front(temp->value);
  29.                 temp = temp->prev;
  30.             }
  31.         }
  32.         return *this;
  33.     }
  34.  
  35.     List& operator=(List&& other) {
  36.         if (this != &other) {
  37.             clear();
  38.             size_ = other.size_;
  39.             head_ = other.head_;
  40.             last_ = other.last_;
  41.             other.head_ = other.last_ = nullptr;
  42.         }
  43.         return *this;
  44.     }
  45.  
  46.     void push_front(const int& value) {
  47.         if (size_ == 0) {
  48.             last_ = head_ = new Node{ value, nullptr, nullptr };
  49.             size_++;
  50.             return;
  51.         }
  52.         Node* temp = new Node{ value, head_, nullptr };
  53.         head_->prev = temp;
  54.         head_ = temp;
  55.         size_++;
  56.     }
  57.  
  58.     void push_back(const int& value) {
  59.         if (size_ == 0) {
  60.             last_ = head_ = new Node{ value, nullptr, nullptr };
  61.             size_++;
  62.             return;
  63.         }
  64.         Node* temp = new Node{ value, nullptr, last_};
  65.         last_->next = temp;
  66.         last_ = temp;
  67.         size_++;
  68.     }
  69.  
  70.     void pop_front() {
  71.         if (size_ > 0) {
  72.             if (size_ == 1) {
  73.                 delete head_;
  74.                 last_ = head_ = nullptr;
  75.                 size_--;
  76.                 return;
  77.             }
  78.             Node* temp = head_;
  79.             head_ = head_->next;
  80.             delete temp;
  81.             head_->prev = nullptr;
  82.             size_--;
  83.         }
  84.     }
  85.  
  86.     void pop_back() {
  87.         if (size_ > 0) {
  88.             if (size_ == 1) {
  89.                 delete head_;
  90.                 last_ = head_ = nullptr;
  91.                 size_--;
  92.                 return;
  93.             }
  94.             Node* temp = last_;
  95.             last_ = last_->prev;
  96.             delete temp;
  97.             last_->next = nullptr;
  98.             size_--;
  99.         }
  100.     }
  101.  
  102.     void print() {
  103.         Node* temp = head_;
  104.         while (temp != nullptr) {
  105.             cout << temp->value << " ";
  106.             temp = temp->next;
  107.         }
  108.         cout << endl;
  109.     }
  110.  
  111.     int size() const {
  112.         return size_;
  113.     }
  114.  
  115.     void clear() {
  116.         while (head_ != nullptr) {
  117.             pop_front();
  118.         }
  119.     }
  120.  
  121.     ~List() {
  122.         clear();
  123.     }
  124.  
  125. private:
  126.     struct Node { // двусвязный список состоит из узлов
  127.         int value; // узел хранит информативную часть
  128.         Node* next; // указатель на следующий узел в списке
  129.         Node* prev; // указатель на предыдущий узел
  130.     };
  131.  
  132.     int size_;
  133.     Node* head_;
  134.     Node* last_;
  135. };
  136.  
  137. int main() {
  138.     List list1;
  139.     for (int i = 0; i < 10; i++) {
  140.         list1.push_front(i + 1);
  141.     }
  142.     list1.print();
  143.     cout << list1.size() << endl;
  144.  
  145.     for (int i = 0; i < 3; i++) {
  146.         list1.pop_front();
  147.     }
  148.     list1.print();
  149.     cout << list1.size() << endl;
  150.  
  151.     List list2(list1);
  152.     list2.print();
  153.     cout << list2.size() << endl;
  154.  
  155.     List list3;
  156.     list3 = move(list1);
  157.     list3.print();
  158.  
  159.     list3.push_back(34);
  160.     list3.push_back(35);
  161.     list3.print();
  162.     list3.pop_back();
  163.     list3.print();
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement