Advertisement
35657

Untitled

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