daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstddef>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>
  7. class Node {
  8. public:
  9.     Node() = default;
  10.  
  11.     Node(const T &data, Node *prev, Node *next)
  12.             : data_(data), prev_(prev), next_(next) {}
  13.  
  14.     T data_;
  15.     Node *prev_ = nullptr;
  16.     Node *next_ = nullptr;
  17. };
  18.  
  19. template<typename T>
  20. class ListIterator {
  21. public:
  22.     using Iterator = Node<T> *;
  23.  
  24.     ListIterator(Node<T> *elem, Node<T> *tail)
  25.             : iter_(elem), tail_(tail) {}
  26.  
  27.     ListIterator<T> &operator++() {
  28.         iter_ = iter_->next_;
  29.         return *this;
  30.     }
  31.  
  32.     ListIterator<T> &operator--() {
  33.         if (iter_ == nullptr)
  34.             iter_ = tail_;
  35.         else
  36.             iter_ = iter_->prev_;
  37.         return *this;
  38.     }
  39.  
  40.     T &operator*() {
  41.         return iter_->data_;
  42.     }
  43.  
  44.     const T &operator*() const {
  45.         return iter_->data_;
  46.     }
  47.  
  48.     bool operator==(const ListIterator &other) {
  49.         return iter_ == other.iter_;
  50.     }
  51.  
  52.     bool operator!=(const ListIterator &other) {
  53.         return iter_ != other.iter_;
  54.     }
  55.  
  56. private:
  57.     Iterator iter_;
  58.     Iterator tail_;
  59. };
  60.  
  61. template<typename T>
  62. class List {
  63. public:
  64.     List() : head_(nullptr),
  65.              tail_(nullptr),
  66.              size_(0) {}
  67.  
  68.     List(const List &other) {
  69.         auto tmp = other.head_;
  70.         for (; tmp != nullptr; tmp = tmp->next_) {
  71.             this->push_back(tmp->data_);
  72.         }
  73.  
  74.         size_ = other.size_;
  75.     }
  76.  
  77.     List &operator=(const List &other) {
  78.         Node<T> *cur = nullptr;
  79.         while (head_) {
  80.             cur = head_;
  81.             head_ = head_->next_;
  82.             delete cur;
  83.         }
  84.  
  85.         auto tmp = other.head_;
  86.         for (; tmp != nullptr; tmp = tmp->next_) {
  87.             this->push_back(tmp->data_);
  88.         }
  89.  
  90.         size_ = other.size_;
  91.         return *this;
  92.     }
  93.  
  94.     void push_back(const T &elem) {
  95.         auto adding = new Node<T>(elem, tail_, nullptr);
  96.         tail_ = adding;
  97.         if (adding->prev_) {
  98.             adding->prev_->next_ = adding;
  99.         } else {
  100.             head_ = adding;
  101.         }
  102.         ++size_;
  103.     }
  104.  
  105.     void push_front(const T &elem) {
  106.         auto adding = new Node<T>(elem, nullptr, head_);
  107.  
  108.         adding->prev_ = head_->prev_;
  109.         head_->prev_ = adding;
  110.  
  111.         if (adding->prev_) {
  112.             adding->prev_->next_ = adding;
  113.         } else {
  114.             head_ = adding;
  115.         }
  116.         ++size_;
  117.     }
  118.  
  119.     void del(Node<T> *nd) {
  120.         if (nd != nullptr) {
  121.             if (nd->prev_)
  122.                 nd->prev_->next_ = nd->next_;
  123.  
  124.             if (nd->next_)
  125.                 nd->next_->prev_ = nd->prev_;
  126.  
  127.             if (nd == head_)
  128.                 head_ = nd->next_;
  129.  
  130.             if (nd == tail_)
  131.                 tail_ = nd->prev_;
  132.  
  133.             delete nd;
  134.  
  135.             size_--;
  136.         }
  137.     }
  138.  
  139.     void pop_back() {
  140.         del(tail_);
  141.     }
  142.  
  143.     void pop_front() {
  144.         del(head_);
  145.     }
  146.  
  147.     size_t size() const {
  148.         return size_;
  149.     }
  150.  
  151.     bool empty() const {
  152.         return !size_;
  153.     }
  154.  
  155.     ListIterator<T> begin() {
  156.         return ListIterator<T>(head_, tail_);
  157.     }
  158.  
  159.     ListIterator<T> end() {
  160.         return ListIterator<T>(tail_->next_, tail_);
  161.     }
  162.  
  163.     ~List() {
  164.         Node<T> *cur(head_);
  165.         while (cur) {
  166.             auto nxt(cur->next_);
  167.             delete cur;
  168.             cur = nxt;
  169.         }
  170.     }
  171.  
  172. private:
  173.     Node<T> *head_ = nullptr;
  174.     Node<T> *tail_ = nullptr;
  175.     size_t size_;
  176. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top