Advertisement
playerr17

List

Sep 22nd, 2022 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.02 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cstdlib>
  3.  
  4. struct ListNode {
  5.     int value;
  6.  
  7.     ListNode* prev = nullptr;
  8.     ListNode* next = nullptr;
  9. };
  10.  
  11. class List {
  12. public:
  13.     List() {
  14.         head_ = nullptr;
  15.         tail_ = nullptr;
  16.         size_ = 0;
  17.     }
  18.  
  19.     List(const List& other_list) {
  20.         size_ = other_list.size();
  21.         if (other_list.empty()) {
  22.             List();
  23.             return;
  24.         }
  25.         ListNode* node = other_list.head_;
  26.         head_ = new ListNode{node->value};
  27.         ListNode* prev_node = head_;
  28.         while (node != nullptr) {
  29.             ListNode* cur_node = new ListNode{node->value};
  30.             LinkAfter(cur_node, prev_node);
  31.             prev_node = cur_node;
  32.             node = node->next;
  33.         }
  34.         tail_ = prev_node;
  35.     }
  36.  
  37.     List& operator=(const List& other_list) {
  38.         size_ = other_list.size();
  39.         if (other_list.empty()) {
  40.             head_ = nullptr;
  41.             tail_ = nullptr;
  42.         } else {
  43.             ListNode* node = other_list.head_;
  44.             head_ = new ListNode{node->value};
  45.             ListNode* prev_node = head_;
  46.             while (node->next != nullptr) {
  47.                 node = node->next;
  48.                 ListNode* cur_node = new ListNode{node->value};
  49.                 LinkAfter(cur_node, prev_node);
  50.                 prev_node = cur_node;
  51.             }
  52.             tail_ = prev_node;
  53.         }
  54.         return *this;
  55.     }
  56.  
  57.     bool empty() const {
  58.         return size_ > 0;
  59.     }
  60.  
  61.     size_t size() const {
  62.         return size_;
  63.     }
  64.  
  65.     void PushBack(int x) {
  66.         ListNode* new_node = new ListNode{x};
  67.         LinkAfter(new_node, tail_);
  68.         tail_ = new_node;
  69.     }
  70.  
  71.     void PushFront(int x) {
  72.         ListNode* new_node = new ListNode{x};
  73.         new_node->next = head_->next;
  74.         LinkAfter(head_, new_node);
  75.         head_ = new_node;
  76.     }
  77.  
  78.     int PopBack() {
  79.         ListNode* copy_tail = tail_;
  80.         Unlink(tail_);
  81.         tail_ = copy_tail->prev;
  82.         return copy_tail->value;
  83.     }
  84.  
  85.     int PopFront() {
  86.         ListNode* copy_head = head_;
  87.         Unlink(head_);
  88.         head_ =  copy_head->next;
  89.         return copy_head->value;
  90.     }
  91.  
  92.     const int& Back() const {
  93.         return tail_->value;
  94.     }
  95.  
  96.     int& Back() {
  97.         return tail_->value;
  98.     }
  99.  
  100.     const int& Front() const {
  101.         return head_->value;
  102.     }
  103.  
  104.     int& Front() {
  105.         return head_->value;
  106.     }
  107.  
  108. private:
  109.     ListNode* head_;
  110.     ListNode* tail_;
  111.     std::size_t size_;
  112.  
  113.     void Unlink(ListNode* node) {
  114.         ListNode* prev = node->prev;
  115.         ListNode* next = node->next;
  116.         prev->next = next;
  117.         next->prev = prev;
  118.     } // удаляет ноду из списка
  119.  
  120.     void LinkAfter(ListNode* target, ListNode* after) {
  121.         ListNode* next = after->next;
  122.         target->next = next;
  123.         after->next = target;
  124.         target->prev = after;
  125.     } // добавляет в список элемент after после элемента target
  126. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement