Advertisement
Guest User

List

a guest
Mar 23rd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Element {
  6.     Element *previous = nullptr;
  7.     Element *next = nullptr;
  8.     bool is_begin = false;
  9.     bool is_end = false;
  10.     int value = -991231231;
  11. };
  12.  
  13.  
  14. class CustomIterator {
  15. private:
  16.     Element *ptr;
  17.  
  18. public:
  19.     explicit CustomIterator(Element *other_ptr) {
  20.         ptr = other_ptr;
  21.     }
  22.  
  23.     CustomIterator &operator++() {
  24.         ptr = ptr->next;
  25.         return *this;
  26.     }
  27.  
  28.     CustomIterator &operator--() {
  29.         ptr = ptr->previous;
  30.         return *this;
  31.     }
  32.  
  33.     bool operator==(const CustomIterator &rhs) const {
  34.         return ptr->previous == rhs.ptr->previous &&
  35.                ptr->next == rhs.ptr->next && ptr->value == rhs.ptr->value;
  36.     }
  37.  
  38.     bool operator!=(const CustomIterator &rhs) const {
  39.         return !(rhs == *this);
  40.     }
  41.  
  42.     int operator*() {
  43.         return ptr->value;
  44.     }
  45. };
  46.  
  47.  
  48. class List {
  49. private:
  50.     Element *begin_ptr;
  51.     Element *end_ptr;
  52.     size_t size_list = 0;
  53.  
  54. public:
  55.     List() {
  56.         begin_ptr = new Element;
  57.         begin_ptr->is_begin = true;
  58.         end_ptr = new Element;
  59.         end_ptr->is_end = true;
  60.         begin_ptr->next = end_ptr;
  61.         end_ptr->previous = begin_ptr;
  62.     }
  63.  
  64.     List &operator=(const List &other) {
  65.         for (auto it = other.begin(); it != other.end(); ++it) {
  66.             push_back(*it);
  67.         }
  68.         return *this;
  69.     }
  70.  
  71.     void push_back(int elem) {
  72.         auto *temp = new Element;
  73.         end_ptr->value = elem;
  74.         end_ptr->next = temp;
  75.         end_ptr->is_end = false;
  76.         temp->is_end = true;
  77.         temp->previous = end_ptr;
  78.         end_ptr = temp;
  79.         size_list++;
  80.     }
  81.  
  82.     void push_front(int elem) {
  83.         auto *temp = new Element;
  84.         begin_ptr->value = elem;
  85.         begin_ptr->previous = temp;
  86.         begin_ptr->is_begin = false;
  87.         temp->is_begin = true;
  88.         temp->next = begin_ptr;
  89.         begin_ptr = temp;
  90.         size_list++;
  91.     }
  92.  
  93.     void pop_back() {
  94.         auto temp = end_ptr->previous;
  95.         end_ptr->previous = temp->previous;
  96.         end_ptr->previous->next = end_ptr;
  97.         delete (temp);
  98.         size_list--;
  99.     }
  100.  
  101.     void pop_front() {
  102.         auto temp = begin_ptr->next;
  103.         begin_ptr->next = begin_ptr->next->next;
  104.         begin_ptr->next->previous = begin_ptr;
  105.         delete (temp);
  106.         size_list--;
  107.     }
  108.  
  109.     size_t size() const {
  110.         return size_list;
  111.     }
  112.  
  113.     CustomIterator begin() const {
  114.         CustomIterator a(begin_ptr);
  115.         if (size_list > 0) {
  116.             ++a;
  117.         }
  118.         return a;
  119.     }
  120.  
  121.     CustomIterator end() const {
  122.         CustomIterator a(end_ptr);
  123.         return a;
  124.     }
  125.  
  126.     ~List() {
  127.         auto act = begin_ptr;
  128.         auto next = act->next;
  129.         while (next != nullptr) {
  130.             delete act;
  131.             act = next;
  132.             next = act->next;
  133.         }
  134.         delete act;
  135.     }
  136. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement