Advertisement
coloriot

HA17

Jul 25th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class List {
  4. private:
  5.     class Node {
  6.     public:
  7.         Node(int val) : value(val), next(nullptr), prev(nullptr) {}
  8.  
  9.         int value;
  10.         Node* next;
  11.         Node* prev;
  12.     };
  13.  
  14.     Node* start;
  15.     Node* finish;
  16.     int size;
  17.  
  18. public:
  19.     List() : start(nullptr), finish(nullptr), size(0) {}
  20.  
  21.     List(const List& other) : start(nullptr), finish(nullptr), size(0) {
  22.         Node* current = other.start;
  23.         while (current) {
  24.             push_back(current->value);
  25.             current = current->next;
  26.         }
  27.     }
  28.  
  29.     ~List() {
  30.         while (size > 0) {
  31.             pop_front();
  32.         }
  33.     }
  34.  
  35.     // Оператор =, сделан из кода конструктора и деструктора
  36.     void operator = (const List& other) {
  37.         if (this == &other) {
  38.             return;
  39.         }
  40.         while (size > 0) {
  41.             pop_front();
  42.         }
  43.         Node* current = other.start;
  44.         while (current) {
  45.             push_back(current->value);
  46.             current = current->next;
  47.         }
  48.     }
  49.  
  50.     void push_back(int new_value) {
  51.         Node* new_node = new Node(new_value);
  52.         if (size > 0) {
  53.             finish->next = new_node;
  54.             new_node->prev = finish;
  55.         } else {
  56.             start = new_node;
  57.         }
  58.         finish = new_node;
  59.         ++size;
  60.     }
  61.  
  62.     void push_front(int new_value) {
  63.         Node* new_node = new Node(new_value);
  64.         new_node->next = start;
  65.         if (start != nullptr) {
  66.             start->prev = new_node;
  67.         }
  68.         start = new_node;
  69.         if (size == 0) {
  70.             finish = new_node;
  71.         }
  72.         ++size;
  73.     }
  74.  
  75.     void pop_front() {
  76.         if (size == 0) {
  77.             return;
  78.         }
  79.         Node* old_start = start;
  80.         start = start->next;
  81.         if (start != nullptr) {
  82.             start->prev = nullptr;
  83.         } else {
  84.             finish = nullptr;
  85.         }
  86.         delete old_start;
  87.         --size;
  88.     }
  89.  
  90.     void pop_back() {
  91.         if (size == 0) {
  92.             return;
  93.         }
  94.         Node* old_finish = finish;
  95.         finish = finish->prev;
  96.         if (finish != nullptr) {
  97.             finish->next = nullptr;
  98.         } else {
  99.             start = nullptr;
  100.         }
  101.         delete old_finish;
  102.         --size;
  103.     }
  104.  
  105.     Node* getStart() const { return start; }
  106.     int getSize() const { return size; }
  107.  
  108.     // Вот здесь я помучался, но нужно именно для этого класса, не для List
  109.     friend std::istream& operator >> (std::istream& in, List& list) {
  110.         int num_elements, value;
  111.         in >> num_elements;
  112.         for (int i = 0; i < num_elements; ++i) {
  113.             in >> value;
  114.             list.push_back(value);
  115.         }
  116.         return in;
  117.     }
  118.  
  119.     friend std::ostream& operator << (std::ostream& out, const List& list) {
  120.         for (List::Node* current = list.start; current != nullptr; current = current->next) {
  121.             out << current->value << " "; // Чисто косметическое;
  122.         }
  123.         return out;
  124.     }
  125. };
  126.  
  127. int main() {
  128.     List list;
  129.     std::cout << "Введите количество элементов и сами элементы: ";
  130.     std::cin >> list;
  131.  
  132.     List copied_list(list);
  133.  
  134.     int n, m;
  135.     std::cout << "push front: ";
  136.     std::cin >> n;
  137.     list.push_front(n);
  138.  
  139.     std::cout << "push back: ";
  140.     std::cin >> m;
  141.     list.push_back(m);
  142.  
  143.     std::cout << "pop front" << std::endl;
  144.     list.pop_front();
  145.  
  146.     std::cout << "pop back" << std::endl;
  147.     list.pop_back();
  148.  
  149.     List another_copied_list(list);
  150.  
  151.     std::cout << "Оригинальный список: " << copied_list << std::endl;
  152.     std::cout << "Список: " << list << std::endl;
  153.     std::cout << "Еще один список: " << another_copied_list << std::endl;
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement