Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 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.  
  26.     ListNodeBase* seekLast() const {
  27.         if (head == nullptr) return nullptr;
  28.         ListNodeBase* it = head;
  29.         while (it->next != nullptr) {
  30.             it = it->next;
  31.         }
  32.         return it;
  33.     }
  34.  
  35.     public:
  36.     class ListIterator {
  37.     private:
  38.         ListNodeBase* current;
  39.  
  40.     public:
  41.         explicit ListIterator(ListNodeBase* cur)
  42.             : current(cur)
  43.         {}
  44.         const  T& operator * () const {
  45.             return static_cast<ListNode*>(current)->value;
  46.         }
  47.         T& operator * () {
  48.             return static_cast<ListNode*>(current)->value;
  49.         }
  50.         ListIterator& operator++ () {
  51.             current = current->next;
  52.             return *this;
  53.         }
  54.         ListIterator& operator-- () {
  55.             current = current->prev;
  56.             return *this;
  57.         }
  58.         bool operator == (const ListIterator& other) const {
  59.             return current == other.current;
  60.         }
  61.         bool operator != (const ListIterator& other) const {
  62.             return current != other.current;
  63.         }
  64.     };
  65.  
  66.     List() : count(0), head(nullptr)
  67.     {}
  68.  
  69.     List(const List &other) : count(other.count) {
  70.         if (!count) return;
  71.         head = static_cast<ListNodeBase*>
  72.         (new ListNode(static_cast<ListNode*>(other.head)->value));
  73.         ListNodeBase* cur = head;
  74.         ListNodeBase* it = other.head->next;
  75.         while (it != nullptr) {
  76.             ListNodeBase *nxt = static_cast<ListNodeBase*>
  77.             (new ListNode(static_cast<ListNode*>(it)->value));
  78.             cur->next = nxt;
  79.             nxt->prev = cur;
  80.             cur = cur->next;
  81.             it = it->next;
  82.         }
  83.     }
  84.  
  85.     List& operator=(const List &other) {
  86.         ListNodeBase *it1 = head;
  87.         while (it1 != nullptr) {
  88.             ListNodeBase *nxt = it1->next;
  89.             delete static_cast<ListNode*>(it1);
  90.             it1 = nxt;
  91.         }
  92.         count = other.count;
  93.         head = static_cast<ListNodeBase*>(new ListNode(static_cast<ListNode*>(other.head)->value));
  94.         ListNodeBase* cur = head;
  95.         ListNodeBase* it = other.head->next;
  96.         while (it != nullptr) {
  97.             ListNodeBase *nxt = static_cast<ListNodeBase*>
  98.             (new ListNode(static_cast<ListNode*>(it)->value));
  99.             cur->next = nxt;
  100.             nxt->prev = cur;
  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);
  123.     }
  124.     ListIterator end() const {
  125.         return ListIterator(nullptr);
  126.     }
  127.  
  128.     void push_back(const T &value) {
  129.         if (head == nullptr) {
  130.             head = static_cast<ListNodeBase*>(new ListNode(value));
  131.             count++;
  132.             return;
  133.         }
  134.         ListNodeBase *it = seekLast();
  135.         ListNodeBase *nlast = static_cast<ListNodeBase*>(new ListNode(value));
  136.         nlast->prev = it;
  137.         it->next = nlast;
  138.         count++;
  139.     }
  140.  
  141.     void push_front(const T &value) {
  142.         if (head == nullptr) {
  143.             head = static_cast<ListNodeBase*>(new ListNode(value));
  144.             count++;
  145.             return;
  146.         }
  147.         ListNodeBase *nprev = static_cast<ListNodeBase*>(new ListNode(value));
  148.         nprev->next = head;
  149.         head->prev = nprev;
  150.         head = nprev;
  151.         count++;
  152.     }
  153.  
  154.     void pop_back() {
  155.         if (head == nullptr) return;
  156.         ListNodeBase *last = seekLast();
  157.         if (last == head) {
  158.             delete static_cast<ListNode*>(head);
  159.             head = nullptr;
  160.             count = 0;
  161.             return;
  162.         }
  163.         last->prev->next = nullptr;
  164.         delete static_cast<ListNode*>(last);
  165.         count--;
  166.     }
  167.  
  168.     void pop_front() {
  169.         if (head == nullptr) return;
  170.         ListNodeBase *nxt = head->next;
  171.         if (nxt != nullptr) {
  172.             nxt->prev = nullptr;
  173.         }
  174.         delete static_cast<ListNode*>(head);
  175.         head = nxt;
  176.         count--;
  177.     }
  178. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement