Advertisement
35657

Untitled

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