35657

Untitled

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