Advertisement
35657

Untitled

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