Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template<typename T>
  4. class List {
  5. private:
  6.     class ListNodeBase {
  7.     public:
  8.         ListNodeBase* next;
  9.         ListNodeBase* prev;
  10.         ListNodeBase()
  11.             : next(nullptr)
  12.             , prev(nullptr)
  13.         {}
  14.     };
  15.     class ListNode: public ListNodeBase {
  16.     public:
  17.         T value;
  18.         explicit ListNode(const T& val)
  19.             :value(val)
  20.         {}
  21.         ListNode(const ListNode &node) : value(node.value) {}
  22.     };
  23.     size_t count;
  24.     ListNodeBase *head;
  25.     ListNodeBase *tail;
  26.  
  27.     public:
  28.     class ListIterator {
  29.     private:
  30.         ListNodeBase* current;
  31.         const List* Data;
  32.  
  33.     public:
  34.         explicit ListIterator(ListNodeBase* cur, const List* data)
  35.             : current(cur)
  36.             , Data(data)
  37.         {}
  38.         const  T& operator * () const {
  39.             return static_cast<ListNode*>(current)->value;
  40.         }
  41.         T& operator * () {
  42.             return static_cast<ListNode*>(current)->value;
  43.         }
  44.         ListIterator& operator++ () {
  45.             current = current->next;
  46.             return *this;
  47.         }
  48.         ListIterator& operator-- () {
  49.             if (current == nullptr) {
  50.                 *this = ListIterator(Data->tail, Data);
  51.                 return *this;
  52.             }
  53.             current = current->prev;
  54.             return *this;
  55.         }
  56.         bool operator == (const ListIterator& other) const {
  57.             return current == other.current;
  58.         }
  59.         bool operator != (const ListIterator& other) const {
  60.             return current != other.current;
  61.         }
  62.     };
  63.  
  64.     List() : count(0), head(nullptr), tail(nullptr)
  65.     {}
  66.  
  67.     List(const List &other) : count(other.count) {
  68.         if (!count) return;
  69.         head = static_cast<ListNodeBase*>
  70.         (new ListNode(static_cast<ListNode*>(other.head)->value));
  71.         ListNodeBase* cur = head;
  72.         ListNodeBase* it = other.head->next;
  73.         while (it != nullptr) {
  74.             ListNodeBase *nxt = static_cast<ListNodeBase*>
  75.             (new ListNode(static_cast<ListNode*>(it)->value));
  76.             cur->next = nxt;
  77.             nxt->prev = cur;
  78.             tail = it;
  79.             cur = cur->next;
  80.             it = it->next;
  81.         }
  82.     }
  83.  
  84.     List& operator=(const List &other) {
  85.         ListNodeBase *it1 = head;
  86.         while (it1 != nullptr) {
  87.             ListNodeBase *nxt = it1->next;
  88.             delete static_cast<ListNode*>(it1);
  89.             it1 = nxt;
  90.         }
  91.         count = other.count;
  92.         head = static_cast<ListNodeBase*>(new ListNode(static_cast<ListNode*>(other.head)->value));
  93.         ListNodeBase* cur = head;
  94.         ListNodeBase* it = other.head->next;
  95.         while (it != nullptr) {
  96.             ListNodeBase *nxt = static_cast<ListNodeBase*>
  97.             (new ListNode(static_cast<ListNode*>(it)->value));
  98.             cur->next = nxt;
  99.             nxt->prev = cur;
  100.             tail = it;
  101.             cur = cur->next;
  102.             it = it->next;
  103.         }
  104.  
  105.         return *this;
  106.     }
  107.  
  108.     ~List() {
  109.         ListNodeBase *it = head;
  110.         while (it != nullptr) {
  111.             ListNodeBase *nxt = it->next;
  112.             delete static_cast<ListNode*>(it);
  113.             it = nxt;
  114.         }
  115.     }
  116.  
  117.     size_t size() const {
  118.         return count;
  119.     }
  120.  
  121.     ListIterator begin() const {
  122.         return ListIterator(head, this);
  123.     }
  124.     ListIterator end() const {
  125.         return ListIterator(nullptr, this);
  126.     }
  127.  
  128.     void push_back(const T &value) {
  129.         if (head == nullptr) {
  130.             tail = head = static_cast<ListNodeBase*>(new ListNode(value));
  131.             count++;
  132.             return;
  133.         }
  134.         ListNodeBase *it = tail;
  135.         ListNodeBase *nlast = static_cast<ListNodeBase*>(new ListNode(value));
  136.         nlast->prev = it;
  137.         it->next = nlast;
  138.         tail = nlast;
  139.         count++;
  140.     }
  141.  
  142.     void push_front(const T &value) {
  143.         if (head == nullptr) {
  144.             head = static_cast<ListNodeBase*>(new ListNode(value));
  145.             count++;
  146.             return;
  147.         }
  148.         ListNodeBase *nprev = static_cast<ListNodeBase*>(new ListNode(value));
  149.         nprev->next = head;
  150.         head->prev = nprev;
  151.         head = nprev;
  152.         count++;
  153.     }
  154.  
  155.     void pop_back() {
  156.         if (head == nullptr) return;
  157.         ListNodeBase *last = tail;
  158.         if (last == head) {
  159.             delete static_cast<ListNode*>(head);
  160.             head = nullptr;
  161.             tail = nullptr;
  162.             count = 0;
  163.             return;
  164.         }
  165.         last->prev->next = nullptr;
  166.         tail = last->prev;
  167.         delete static_cast<ListNode*>(last);
  168.         count--;
  169.     }
  170.  
  171.     void pop_front() {
  172.         if (head == nullptr) return;
  173.         ListNodeBase *nxt = head->next;
  174.         if (nxt != nullptr) {
  175.             nxt->prev = nullptr;
  176.         }
  177.         delete static_cast<ListNode*>(head);
  178.         head = nxt;
  179.         count--;
  180.         if (count == 0) {
  181.             tail = nullptr;
  182.         }
  183.     }
  184. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement