35657

Untitled

Jun 23rd, 2023
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.88 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 Insert(const int index, const int& value) {
  124.         if (index == 0) {
  125.             Push_front(value);
  126.             return;
  127.         }
  128.         if (index == size) {
  129.             Push_back(value);
  130.             return;
  131.         }
  132.         if (index > 0 && index < size) {
  133.             Node* temp = head;
  134.             for (int i = 0; i < index - 1; i++) {
  135.                 temp = temp->next;
  136.             }
  137.             Node* node = new Node{ value, temp->next, temp };
  138.             temp->next->prev = node;
  139.             temp->next = node;
  140.             size++;
  141.         }
  142.     }
  143.  
  144.     void Erase(const int index) {
  145.         if (index == 0) {
  146.             Pop_front();
  147.             return;
  148.         }
  149.         if (index == size - 1) {
  150.             Pop_back();
  151.             return;
  152.         }
  153.         if (index > 0 && index < size - 1) {
  154.             Node* temp = head;
  155.             for (int i = 0; i < index - 1; i++) {
  156.                 temp = temp->next;
  157.             }
  158.             Node* buf = temp->next->next;
  159.             delete temp->next;
  160.             temp->next = buf;
  161.             temp->next->prev = temp;
  162.             size--;
  163.         }
  164.     }
  165.  
  166.     void Print() {
  167.         Node* temp = head;
  168.         while (temp != nullptr) {
  169.             cout << temp->value << " ";
  170.             temp = temp->next;
  171.         }
  172.         cout << endl;
  173.     }
  174.  
  175.     T& Front() {
  176.         if (head != nullptr) {
  177.             return head->value;
  178.         }
  179.     }
  180.  
  181.     T& Front() const {
  182.         if (head != nullptr) {
  183.             return head->value;
  184.         }
  185.     }
  186.  
  187.     T& Back() {
  188.         if (last != nullptr) {
  189.             return last->value;
  190.         }
  191.     }
  192.  
  193.     T& Back() const {
  194.         if (last != nullptr) {
  195.             return last->value;
  196.         }
  197.     }
  198.  
  199.     int Size() const {
  200.         return size;
  201.     }
  202.  
  203.     void Clear() {
  204.         while (head != nullptr) {
  205.             Pop_front();
  206.         }
  207.     }
  208.  
  209.     T& operator[] (const int index) {
  210.         if (index >= 0 && index < size) {
  211.             Node* temp = head;
  212.             for (int i = 0; i < index; i++) {
  213.                 temp = temp->next;
  214.             }
  215.             return temp->value;
  216.         }
  217.     }
  218.  
  219.     const T& operator[] (const int index) const {
  220.         if (index >= 0 && index < size) {
  221.             Node* temp = head;
  222.             for (int i = 0; i < index; i++) {
  223.                 temp = temp->next;
  224.             }
  225.             return temp->value;
  226.         }
  227.     }
  228.  
  229.     bool operator==(const List& other) {
  230.         if (this->size != other.size) {
  231.             return false;
  232.         }
  233.         Node* temp = head;
  234.         Node* other_temp = other.head;
  235.         while (temp != nullptr) {
  236.             if (temp->value != other_temp->value) {
  237.                 return false;
  238.             }
  239.             temp = temp->next;
  240.             other_temp = other_temp->next;
  241.         }
  242.         return true;
  243.     }
  244.  
  245.     bool operator!=(const List& other) {
  246.         return !(*this == other);
  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.     int size = 0;
  271.     Node* head = nullptr;
  272.     Node* last = nullptr;
  273. };
  274.  
  275. int main() {
  276.     List<int> list1;
  277.     for (int i = 0; i < 10; i++) {
  278.         list1.Push_front(i + 1);
  279.         list1.Print();
  280.     }
  281.     cout << list1.Size() << endl;
  282.  
  283.     for (int i = 0; i < 3; i++) {
  284.         list1.Pop_front();
  285.         list1.Print();
  286.     }
  287.     cout << list1.Size() << endl;
  288.  
  289.     cout << "Front: " << list1.Front() << " Back: " << list1.Back() << endl;
  290.  
  291.     list1.Push_back(10);
  292.     list1.Print();
  293.     list1.Pop_back();
  294.     list1.Print();
  295.  
  296.     cout << "list2: " << endl;
  297.     List<int> list2(move(list1));
  298.     list2.Print();
  299.  
  300.     cout << "list1: " << endl;
  301.     list1.Print();
  302.  
  303.     list2.Insert(3, 12);
  304.     cout << "list2: " << endl;
  305.     list2.Print();
  306.     list2.Erase(2);
  307.     list2.Print();
  308.     cout << list2.Find(12) << endl;
  309.     cout << list2[3] << endl;
  310.     List<int> list3(list2);
  311.     cout << "list3: " << endl;
  312.     list3.Print();
  313.     list3[3] = 22;
  314.     list3.Print();
  315.     cout << boolalpha;
  316.     cout << (list2 == list3) << endl;
  317.     cout << (list2 != list3) << endl;
  318. }
Advertisement
Add Comment
Please, Sign In to add comment