Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. using T = int;
  4.  
  5. class List;
  6.  
  7. struct Element {
  8. int v;
  9. Element * previous;
  10. Element * next;
  11. Element(const int& v, Element* next = nullptr, Element* previous = nullptr)
  12. : v(v), next(next), previous(previous) {
  13. if (next != nullptr) {
  14. next->previous = this;
  15. }
  16. if (previous != nullptr) {
  17. previous->next = this;
  18. }
  19. }
  20. ~Element() {
  21. if (previous != nullptr) {
  22. previous->next = nullptr;
  23. }
  24. if (next != nullptr) {
  25. next->previous = nullptr;
  26. }
  27. }
  28. };
  29.  
  30. class Iterator {
  31. private:
  32. Element* current;
  33. const List& l;
  34.  
  35. public:
  36. Iterator(Element* current1, const List& l1) : l(l1) {
  37. current = current1;
  38. }
  39. Iterator& operator++() {
  40. current = (*current).previous;
  41. return *this;
  42. }
  43. Iterator operator++(int) {
  44. Iterator temp = *this;
  45. ++(*this);
  46. return temp;
  47. }
  48. Iterator& operator--() {
  49. if (current != nullptr) {
  50. current = (*current).next;
  51. } else {
  52. current = l.Tail();
  53. }
  54. return *this;
  55. }
  56. Iterator operator--(int) {
  57. Iterator temp = *this;
  58. --(*this);
  59. return temp;
  60. }
  61. bool operator==(const Iterator& other) const {
  62. if (current == other.current) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. bool operator!=(const Iterator& other) const {
  68. if (*this == other) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. const int& operator*() const {
  74. return (*current).v;
  75. }
  76. };
  77.  
  78. class List {
  79. public:
  80. size_t ListSize;
  81. Element* head;
  82. Element* tail;
  83. List() {
  84. ListSize = 0;
  85. head = nullptr;
  86. tail = nullptr;
  87. }
  88. List(const List& other) {
  89. ListSize = 0;
  90. head = nullptr;
  91. tail = nullptr;
  92. for (const auto& v : other) {
  93. ++ListSize;
  94. Element* temp = new Element(v, tail);
  95. tail = temp;
  96. if (head == nullptr) {
  97. head = temp;
  98. }
  99. }
  100. }
  101. List& operator=(const List& other) {
  102. while (head != nullptr) {
  103. Element* temp = head;
  104. head = (*head).previous;
  105. delete temp;
  106. }
  107. ListSize = 0;
  108. tail = nullptr;
  109. List temp(other);
  110. Element * h = head;
  111. head = temp.head;
  112. temp.head = h;
  113. Element * t = tail;
  114. tail = temp.tail;
  115. temp.tail = t;
  116. size_t s = ListSize;
  117. ListSize = temp.ListSize;
  118. temp.ListSize = s;
  119. return *this;
  120. }
  121. size_t size() {
  122. return ListSize;
  123. }
  124. void push_back(const int& v) {
  125. Element * el = new Element(v, tail);
  126. ++ListSize;
  127. if (ListSize == 1) {
  128. head = el;
  129. }
  130. tail = el;
  131. }
  132. Element* Tail() const {
  133. return tail;
  134. }
  135. void pop_back() {
  136. Element * FormerTail = tail;
  137. tail = (*tail).next;
  138. delete FormerTail;
  139. --ListSize;
  140. if (ListSize == 0)
  141. head = tail;
  142. }
  143. void push_front(const int& v) {
  144. Element * el = new Element(v, nullptr, head);
  145. ++ListSize;
  146. if (ListSize == 1) {
  147. tail = el;
  148. }
  149. head = el;
  150. }
  151. void pop_front() {
  152. Element * FormerHead = head;
  153. head = (*head).previous;
  154. delete FormerHead;
  155. --ListSize;
  156. if (ListSize == 0)
  157. tail = head;
  158. }
  159. Iterator begin() const {
  160. return Iterator(head, *this);
  161. }
  162. Iterator end() const {
  163. return Iterator(nullptr, *this);
  164. }
  165. ~List() {
  166. while (head != nullptr) {
  167. Element * temp = head;
  168. head = (*head).previous;
  169. delete temp;
  170. }
  171. }
  172. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement