Advertisement
35657

Untitled

Mar 19th, 2024 (edited)
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node { // односвязный список состоит из узлов
  6.     int value; // узел хранит информативную часть
  7.     Node* next; // указатель на следующий узел
  8. };
  9.  
  10. class ForwardList {
  11. public:
  12.  
  13.     ForwardList() : size_(0), head_(nullptr) {}
  14.  
  15.     ForwardList(const ForwardList& other) : size_(other.size_), head_(nullptr) {
  16.         if (other.head_ != nullptr) {
  17.             head_ = new Node{ other.head_->value, nullptr };
  18.             Node* temp = head_;
  19.             Node* other_temp = other.head_;
  20.             while (other_temp->next != nullptr) {
  21.                 other_temp = other_temp->next;
  22.                 temp->next = new Node{ other_temp->value, nullptr };
  23.                 temp = temp->next;
  24.             }
  25.         }
  26.     }
  27.  
  28.     ForwardList(ForwardList&& other) : size_(other.size_), head_(other.head_) {
  29.         other.head_ = nullptr;
  30.     }
  31.  
  32.     ForwardList& operator=(const ForwardList& other) {
  33.         if (this != &other) {
  34.             clear();
  35.             if (other.head_ != nullptr) {
  36.                 head_ = new Node{ other.head_->value, nullptr };
  37.                 Node* temp = head_;
  38.                 Node* other_temp = other.head_;
  39.                 while (other_temp->next != nullptr) {
  40.                     other_temp = other_temp->next;
  41.                     temp->next = new Node{ other_temp->value, nullptr };
  42.                     temp = temp->next;
  43.                 }
  44.             }
  45.         }
  46.         return *this;
  47.     }
  48.  
  49.  
  50.     ForwardList& operator=(ForwardList&& other) {
  51.         if (this != &other) {
  52.             clear();
  53.             size_ = other.size_;
  54.             head_ = other.head_;
  55.             other.head_ = nullptr;
  56.         }
  57.         return *this;
  58.     }
  59.  
  60.  
  61.     void clear() {
  62.         while (head_ != nullptr) {
  63.             pop_front();
  64.         }
  65.     }
  66.  
  67.     void push_front(const int& value) {
  68.         head_ = new Node{ value, head_ };
  69.         size_++;
  70.     }
  71.  
  72.     void pop_front() {
  73.         if (size_ > 0) {
  74.             Node* temp = head_;
  75.             head_ = head_->next;
  76.             delete temp;
  77.             size_--;
  78.         }
  79.     }
  80.  
  81.     void print() const {
  82.         Node* temp = head_;
  83.         while (temp != nullptr) {
  84.             cout << temp->value << " ";
  85.             temp = temp->next;
  86.         }
  87.         cout << endl;
  88.     }
  89.  
  90.     void insert(const int index, const int value) {
  91.         if (index == 0) {
  92.             push_front(value);
  93.             return;
  94.         }
  95.         if (index > 0 && index <= size_) {
  96.             Node* temp = head_;
  97.             for (int i = 0; i < index - 1; i++) {
  98.                 temp = temp->next;
  99.             }
  100.             temp->next = new Node{ value, temp->next };
  101.             size_++;
  102.         }
  103.     }
  104.  
  105.     void erase(const int index) {
  106.  
  107.     }
  108.  
  109.     int size() const {
  110.         return size_;
  111.     }
  112.  
  113.     ~ForwardList() {
  114.         clear();
  115.     }
  116.  
  117. private:
  118.     int size_;
  119.     Node* head_;
  120. };
  121.  
  122. int main() {
  123.  
  124.     ForwardList list1;
  125.     list1.push_front(35);
  126.     list1.push_front(453);
  127.     list1.push_front(357);
  128.  
  129.     list1.print();
  130.     cout << list1.size() << endl;
  131.  
  132.     ForwardList list2(list1);
  133.  
  134.     ForwardList list3;
  135.  
  136.     list3 = move(list1);
  137.     list3.print();
  138.     list3.insert(2, 563);
  139.     list3.print();
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement