Advertisement
35657

Untitled

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