35657

Untitled

Jun 19th, 2023
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.10 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<typename T>
  6. class List {
  7. public:
  8.  
  9.     struct Node { // двусвязный список состоит из узлов
  10.         T value; // узел хранит информативную часть
  11.         Node* next; // указатель на следующий узел в списке
  12.         Node* prev; // указатель на предыдущий узел
  13.     };
  14.  
  15.     List() : size(0), head(nullptr), last(nullptr) {}
  16.  
  17.     List(const List& other) : size(other.size), head(nullptr), last(nullptr) {
  18.         if (other.head != nullptr) {
  19.             head = new Node{ other.head->value, nullptr, nullptr };
  20.             Node* temp = head;
  21.             Node* other_temp = other.head;
  22.             while (other_temp->next != nullptr) {
  23.                 other_temp = other_temp->next;
  24.                 temp->next = new Node{ other_temp->value, nullptr, temp };
  25.                 temp = temp->next;
  26.             }
  27.             last = temp;
  28.         }
  29.     }
  30.  
  31.     List(List&& other) : size(other.size), head(other.head), last(other.last) {
  32.         other.size = 0;
  33.         other.head = nullptr;
  34.         other.last = nullptr;
  35.     }
  36.  
  37.     List& operator=(const List& other) {
  38.         if (this != &other) {
  39.             Clear();
  40.             if (other.head != nullptr) {
  41.                 head = new Node{ other.head->value, nullptr, nullptr };
  42.                 Node* temp = head;
  43.                 Node* other_temp = other.head;
  44.                 while (other_temp->next != nullptr) {
  45.                     other_temp = other_temp->next;
  46.                     temp->next = new Node{ other_temp->value, nullptr, temp };
  47.                     temp = temp->next;
  48.                 }
  49.                 last = temp;
  50.                 size = other.size;
  51.             }
  52.         }
  53.         return *this;
  54.     }
  55.  
  56.     List& operator=(List&& other) {
  57.         if (this != &other) {
  58.             Clear();
  59.             size = other.size;
  60.             head = other.head;
  61.             last = other.last;
  62.             other.size = 0;
  63.             other.head = nullptr;
  64.             other.last = nullptr;
  65.         }
  66.         return *this;
  67.     }
  68.  
  69.     void Push_front(const T& value) {
  70.         if (size == 0) {
  71.             last = head = new Node{ value, nullptr, nullptr };
  72.             size++;
  73.             return;
  74.         }
  75.         Node* temp = new Node{ value, head, nullptr };
  76.         head->prev = temp;
  77.         head = temp;
  78.         size++;
  79.     }
  80.  
  81.     void Push_back(const T& value) {
  82.         if (size == 0) {
  83.             last = head = new Node{ value, nullptr, nullptr };
  84.             size++;
  85.             return;
  86.         }
  87.         Node* temp = new Node{ value, nullptr, last };
  88.         last->next = temp;
  89.         last = temp;
  90.         size++;
  91.     }
  92.  
  93.     void Pop_front() {
  94.         if (size > 0) {
  95.             if (size == 1) {
  96.                 delete head;
  97.                 last = head = nullptr;
  98.                 size--;
  99.                 return;
  100.             }
  101.             head = head->next;
  102.             delete head->prev;
  103.             head->prev = nullptr;
  104.             size--;
  105.         }
  106.     }
  107.  
  108.     void Pop_back() {
  109.         if (size > 0) {
  110.             if (size == 1) {
  111.                 delete head;
  112.                 last = head = nullptr;
  113.                 size--;
  114.                 return;
  115.             }
  116.             last = last->prev;
  117.             delete last->next;
  118.             last->next = nullptr;
  119.             size--;
  120.         }
  121.     }
  122.  
  123.     void Print() {
  124.         Node* temp = head;
  125.         while (temp != nullptr) {
  126.             cout << temp->value << " ";
  127.             temp = temp->next;
  128.         }
  129.         cout << endl;
  130.     }
  131.  
  132.     T& Front() {
  133.         if (head != nullptr) {
  134.             return head->value;
  135.         }
  136.     }
  137.  
  138.     T& Front() const {
  139.         if (head != nullptr) {
  140.             return head->value;
  141.         }
  142.     }
  143.  
  144.     T& Back() {
  145.         if(last != nullptr) {
  146.             return last->value;
  147.         }
  148.     }
  149.  
  150.     T& Back() const {
  151.         if (last != nullptr) {
  152.             return last->value;
  153.         }
  154.     }
  155.  
  156.     int Size() const {
  157.         return size;
  158.     }
  159.  
  160.     void Clear() {
  161.         while (head != nullptr) {
  162.             Pop_front();
  163.         }
  164.     }
  165.  
  166.     ~List() {
  167.         Clear();
  168.     }
  169.  
  170. private:
  171.     int size = 0;
  172.     Node* head = nullptr;
  173.     Node* last = nullptr;
  174. };
  175.  
  176. int main() {
  177.     List<int> list1;
  178.     for (int i = 0; i < 10; i++) {
  179.         list1.Push_front(i + 1);
  180.         list1.Print();
  181.     }
  182.     cout << list1.Size() << endl;
  183.  
  184.     for (int i = 0; i < 3; i++) {
  185.         list1.Pop_front();
  186.         list1.Print();
  187.     }
  188.     cout << list1.Size() << endl;
  189.  
  190.     cout << "Front: " << list1.Front() << " Back: " << list1.Back() << endl;
  191.  
  192.     list1.Push_back(10);
  193.     list1.Print();
  194.     list1.Pop_back();
  195.     list1.Print();
  196.  
  197.     cout << "list2: " << endl;
  198.     List<int> list2(move(list1));
  199.     list2.Print();
  200.  
  201.     cout << "list1: " << endl;
  202.     list1.Print();
  203. }
  204.  
  205. //Задание: реализовать конструктор перемещения, перемещающий оператор присваивания
Advertisement
Add Comment
Please, Sign In to add comment