SlepoiKrot

Untitled

Oct 18th, 2020
664
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <list>
  2.  
  3. template<typename T>
  4. class List {
  5.     class Node {
  6.         T x;
  7.         Node* left;
  8.         Node* right;
  9.     };
  10.     class Iterator {
  11.     public:
  12.         Iterator();
  13.         Iterator(Node* node);
  14.         const Iterator operator++ (int);
  15.         const Iterator operator-- (int);
  16.         T& operator* ();
  17.         const Iterator operator== (const Iterator& elem);
  18.         const Iterator operator!= (const Iterator& elem);
  19.         ~Iterator() = default;
  20.         Node* current = nullptr;
  21.     };
  22.  
  23. private:
  24.     Node* begin_node;
  25.     Node* end_node;
  26.  
  27. public:
  28.     List();
  29.     void push_back(const T& elem);
  30.     void push_front(const T& elem);
  31.     void pop_back();
  32.     void pop_front();
  33.     size_t size() const;
  34.     Iterator begin();
  35.     Iterator end();
  36.     ~List();
  37. };
  38.  
  39. template<typename T>
  40. List<T>::Iterator::Iterator() {
  41.     current = nullptr;
  42. }
  43.  
  44. template<typename T>
  45. List<T>::Iterator::Iterator(Node* node) {
  46.     current = node;
  47. }
  48.  
  49. template<typename T>
  50. T& List<T>::Iterator::operator*() {
  51.     return *current;
  52. }
  53.  
  54. template<typename T>
  55. const typename List<T>::Iterator List<T>::Iterator::operator++(int) {
  56.     current = Iterator(current->right);
  57.     return *this;
  58. }
  59.  
  60. template<typename T>
  61. const typename List<T>::Iterator List<T>::Iterator::operator--(int) {
  62.     current = Iterator(current->left);
  63.     return *this;
  64. }
  65.  
  66. template<typename T>
  67. typename List<T>::Iterator& List<T>::Iterator::operator==(const Iterator& elem) {
  68.     return elem.current == current;
  69. }
  70.  
  71. template<typename T>
  72. typename List<T>::Iterator& List<T>::Iterator::operator!=(const Iterator& elem) {
  73.     return !(elem.current == *this);
  74. }
  75.  
  76. template<typename T>
  77. void List<T>::push_back(const T& elem) {
  78.     Node* new_cur = new Node(elem);
  79.     end_node->right = new_cur;
  80.     new_cur->left = end_node;
  81.     end_node = new_cur;
  82. }
  83.  
  84. template<typename T>
  85. void List<T>::push_front(const T& elem) {
  86.     Node* new_cur = new Node(elem);
  87.     begin_node->left = new_cur;
  88.     new_cur->right = begin_node;
  89.     begin_node = new_cur;
  90. }
  91.  
  92. template<typename T>
  93. void List<T>::pop_back() {
  94.     Node* new_cur = end_node->left;
  95.     new_cur->right = nullptr;
  96.     end_node = new_cur;
  97. }
  98.  
  99. template<typename T>
  100. List<T>::~List() {
  101.     Node* cur = begin_node;
  102.     Node* prev = nullptr;
  103.     while (cur != nullptr) {
  104.         prev = cur;
  105.         cur = cur->right;
  106.         delete prev;
  107.     }
  108. }
RAW Paste Data