TrickmanOff

LinkedList

Sep 29th, 2020
847
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // !voteban Dmitrii Stasenko :( --
  2. // ++
  3. #include <iostream>
  4.  
  5. template<class T>
  6. class LinkedList {
  7.     class Node {
  8.         T x;
  9.         Node* left;
  10.         Node* right;
  11.     };
  12.     struct Iterator {
  13.         Node* curNode = nullptr;
  14.         Iterator();
  15.         Iterator(Node*);
  16.         T& operator* ();
  17.         ~Iterator() = default;
  18.         Iterator& operator++ ();
  19.         Iterator& operator-- ();
  20.         bool operator== (const Iterator&) const;
  21.         bool operator!= (const Iterator&) const;
  22.     };
  23. public:
  24.     ~LinkedList();
  25.    
  26.     Iterator begin() const;
  27.     Iterator begin();
  28.     Iterator end() const;
  29.     Iterator end();
  30. private:
  31.     Node* begin_node;
  32.     Node* end_node;
  33. public:
  34.    
  35.     /*LinkedList();
  36.     Iterator push_front(const T& elem);
  37.     T pop_front();*/
  38.     Iterator push_back(const T& elem);
  39.     /*T pop_back();
  40.     [[nodiscard]] size_t size() const;  
  41.     Iterator insert(const T& elem, size_t index);
  42.     T front() const;
  43.     T& front();
  44.     T back() const;
  45.     T& back();
  46.     Iterator begin() const;
  47.     Iterator end() const;
  48.     const Iterator begin();
  49.     const Iterator end();*/
  50. };
  51. // напиши рут нод head
  52.  
  53. template<class T>
  54. LinkedList<T>::Iterator LinkedList<T>::Iterator::begin() {
  55.     return Iterator(begin_node);
  56. }
  57.  
  58. template<class T>
  59. LinkedList<T>::Iterator LinkedList<T>::Iterator::end() {
  60.     return Iterator(end_node->right);
  61. }
  62.  
  63. template<class T>
  64. LinkedList<T>::Iterator::Iterator() {
  65.     curNode = nullptr;
  66. }
  67.  
  68. template<class T>
  69. LinkedList<T>::Iterator::Iterator(Node* x) {
  70.     curNode = x;
  71. }
  72.  
  73. template<class T>
  74. T& LinkedList<T>::Iterator::operator* () {
  75.     return (*curNode); // curNode->x ?
  76. }
  77.  
  78. template<class T>
  79. bool LinkedList<T>::Iterator::operator== (const Iterator& elem) {
  80.     return elem.curNode == curNode;
  81. }
  82.  
  83. template<class T>
  84. bool LinkedList<T>::Iterator::operator!= (const Iterator& elem) {
  85.     return !(*this == elem);
  86. }
  87.  
  88. template<class T>
  89. LinkedList<T>::Iterator& LinkedList<T>::Iterator::operator++ () {
  90.     curNode = curNode->right;
  91.     return *this;
  92. }
  93.  
  94. template<class T>
  95. LinkedList<T>::Iterator& LinkedList<T>::Iterator::operator-- () {
  96.     curNode = curNode->left;
  97.     return *this;// curNode = curNode->right; return *this
  98. }
  99.  
  100. template<class T>
  101. LinkedList<T>::Iterator LinkedList<T>::push_back(const T& elem) {
  102.     Node* cur = new Node(elem);
  103.     end_node->right = cur;
  104.     cur->left = end_node;
  105.     end_node = cur;
  106.     return Iterator(end_node);
  107. }
  108.  
  109. template<class T>
  110. LinkedList<T>::~LinkedList() {
  111.     Node* ptr = begin_node;
  112.     Node* was;
  113.     while (ptr != nullptr) {
  114.         was = ptr;
  115.         ptr = ptr->right;
  116.         delete was;
  117.     }
  118. }
  119. //напиши !=
  120. int main(){
  121.     LinkedList<int> l;
  122.     for (int i = 0; i < 10; ++i) {
  123.         l.push_back(i);
  124.     }
  125.     int count = 0;
  126.     for (auto it = l.begin(); it != l.end(); ++it) {
  127.         ++count;
  128.     }
  129.     std::cout << count << '\n';
  130.     std::cout << "Nice\n";
  131. }
  132.  
RAW Paste Data