Advertisement
35657

Untitled

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