35657

Untitled

Sep 7th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.95 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 initializer_list<T>& list) : size(0), head(nullptr), last(nullptr) {
  12.         for (const auto& element : list) {
  13.             Push_back(element);
  14.         }
  15.     }
  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() const {
  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.     const 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.     const 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.     void Insert(const int index, const int& value) {
  167.         if (index == 0) {
  168.             Push_front(value);
  169.             return;
  170.         }
  171.         if (index == size) {
  172.             Push_back(value);
  173.             return;
  174.         }
  175.         if (index > 0 && index < size) {
  176.             Node* temp = head;
  177.             for (int i = 0; i < index - 1; i++) {
  178.                 temp = temp->next;
  179.             }
  180.             Node* node = new Node{ value, temp->next, temp };
  181.             temp->next->prev = node;
  182.             temp->next = node;
  183.             size++;
  184.         }
  185.     }
  186.  
  187.     void Erase(const int index) {
  188.         if (index == 0) {
  189.             Pop_front();
  190.             return;
  191.         }
  192.         if (index == size) {
  193.             Pop_back();
  194.             return;
  195.         }
  196.         if (index > 0 && index < size) {
  197.             Node* temp = head;
  198.             for (int i = 0; i < index - 1; i++) {
  199.                 temp = temp->next;
  200.             }
  201.             Node* buf = temp->next->next;
  202.             delete temp->next;
  203.             temp->next = buf;
  204.             temp->next->prev = temp;
  205.             size--;
  206.         }
  207.     }
  208.  
  209.     bool operator==(const List& other) {
  210.         if (this->size != other.size) {
  211.             return false;
  212.         }
  213.         Node* temp = head;
  214.         Node* other_temp = other.head;
  215.         while (temp != nullptr) {
  216.             if (temp->value != other_temp->value) {
  217.                 return false;
  218.             }
  219.             temp = temp->next;
  220.             other_temp = other_temp->next;
  221.         }
  222.         return true;
  223.     }
  224.  
  225.     bool operator!=(const List& other) {
  226.         return !(*this == other);
  227.     }
  228.  
  229.     T& operator[] (const int index) {
  230.         if (index >= 0 && index < size) {
  231.             Node* temp = head;
  232.             for (int i = 0; i < index; i++) {
  233.                 temp = temp->next;
  234.             }
  235.             return temp->value;
  236.         }
  237.     }
  238.  
  239.     const T& operator[] (const int index) const {
  240.         if (index >= 0 && index < size) {
  241.             Node* temp = head;
  242.             for (int i = 0; i < index; i++) {
  243.                 temp = temp->next;
  244.             }
  245.             return temp->value;
  246.         }
  247.     }
  248.  
  249.     int Find(const T& value) {
  250.         if (size == 0) {
  251.             return -1;
  252.         }
  253.         int i = 0;
  254.         Node* temp = head;
  255.         while (temp != nullptr && temp->value != value) {
  256.             temp = temp->next;
  257.             i++;
  258.         }
  259.         if (temp != nullptr) {
  260.             return i;
  261.         }
  262.         return -1;
  263.     }
  264.  
  265.     ~List() {
  266.         Clear();
  267.     }
  268.  
  269. private:
  270.     struct Node { // двусвязный список состоит из узлов
  271.         T value; // узел хранит информативную часть
  272.         Node* next; // указатель на следующий узел в списке
  273.         Node* prev; // указатель на предыдущий узел
  274.     };
  275.  
  276.     int size = 0;
  277.     Node* head = nullptr;
  278.     Node* last = nullptr;
  279. };
  280.  
  281. int main() {
  282.     List<int> list1;
  283.     for (int i = 0; i < 10; i++) {
  284.         list1.Push_front(i + 1);
  285.         list1.Print();
  286.     }
  287.     cout << list1.Size() << endl;
  288.  
  289.     for (int i = 0; i < 3; i++) {
  290.         list1.Pop_front();
  291.         list1.Print();
  292.     }
  293.     cout << list1.Size() << endl;
  294.  
  295.     cout << "Front: " << list1.Front() << " Back: " << list1.Back() << endl;
  296.  
  297.     list1.Push_back(10);
  298.     list1.Print();
  299.     list1.Pop_back();
  300.     list1.Print();
  301.  
  302.     cout << "list2: " << endl;
  303.     List<int> list2(move(list1));
  304.     list2.Print();
  305.  
  306.     cout << "list1: " << endl;
  307.     list1.Print();
  308.  
  309.     list2.Insert(3, 12);
  310.     cout << "list2: " << endl;
  311.     list2.Print();
  312.  
  313.     list2.Erase(2);
  314.     list2.Print();
  315.     cout << list2.Find(12) << endl;
  316.     cout << boolalpha << (list1 == list2) << endl;
  317.     cout << (list1 != list2) << endl;
  318.  
  319.     cout << list2[3] << endl;
  320.     list2[3] = 10;
  321.     list2.Print();
  322. }
Add Comment
Please, Sign In to add comment