Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. // ListDeque.h
  2. #ifndef ListDequeH
  3. #define ListDequeH
  4. // ---------------------------------------------------------------------------
  5.  
  6. #include <stdexcept>
  7.  
  8. template<typename T>
  9. class ListDeque {
  10.     /* header:
  11.  
  12.      // construct/copy/destroy:
  13.      ListDeque(const ListDeque& x);
  14.      ListDeque(ListDeque&& x);
  15.      ~ListDeque();
  16.      ListDeque& operator=(const ListDeque& x);
  17.      ListDeque& operator=(ListDeque&& x);
  18.  
  19.      // capacity:
  20.      int size() const;
  21.      bool empty() const;
  22.  
  23.      // element access:
  24.      T& operator[](int n);
  25.      const T& operator[](int n) const;
  26.      T& at(int n);
  27.      const T& at(int n) const;
  28.      T& front();
  29.      const T& front() const;
  30.      T& back();
  31.      const T& back() const;
  32.  
  33.      // modifiers:
  34.      void push_front(const T& x);
  35.      void push_front(T&& x);
  36.      void push_back(const T& x);
  37.      void push_back(T&& x);
  38.      void pop_front();
  39.      void pop_back();
  40.      void swap(ListDeque& x);
  41.      void swap(ListDeque& x, ListDeque& y);
  42.      void clear();
  43.  
  44.      // operators
  45.      bool operator==(const ListDeque& x) const;
  46.      bool operator!=(const ListDeque& x) const;
  47.  
  48.      */
  49.  
  50.     public:
  51.     class Node {
  52.         public:
  53.         T value;
  54.         Node* next;
  55.         Node* prev;
  56.  
  57.         Node(const T value, Node* prev = nullptr, Node* next = nullptr)
  58.             : value(value), prev(prev), next(next) {}
  59.  
  60.         Node(T && value, Node* prev = nullptr, Node* next = nullptr)
  61.             : value(value), prev(prev), next(next) {}
  62.     };
  63.  
  64.     int count = 0;
  65.     Node* first = nullptr;
  66.     Node* last = nullptr;
  67.  
  68.     // construct/copy/destroy:
  69.     ListDeque() = default;
  70.  
  71.     ListDeque(const ListDeque& x) {
  72.         for (Node * n = x.first; n; n = n->next) {
  73.             push_front(n->value);
  74.         }
  75.     }
  76.  
  77.     ListDeque(ListDeque && x) {
  78.         swap(x);
  79.     }
  80.  
  81.     ~ListDeque() {
  82.         clear();
  83.     }
  84.     ListDeque& operator = (const ListDeque & x) {
  85.         for (Node * n = x.first; n; n = n->next) {
  86.             push_front(n->value);
  87.         }
  88.         return* this;
  89.     }
  90.     ListDeque& operator = (ListDeque && x) {
  91.         swap(x);
  92.         return* this;
  93.     }
  94.  
  95.     // capacity:
  96.     int size() const {
  97.         return count;
  98.     }
  99.  
  100.     bool empty() const {
  101.         return !count;
  102.     }
  103.  
  104.     // element access:
  105.     T& operator[](int n) {
  106.         Node* x = first;
  107.         while (x && n-- > 0) {
  108.             x = x->next;
  109.         }
  110.         return x->value;
  111.     }
  112.  
  113.     const T& operator[](int n) const {
  114.         Node* x = first;
  115.         while (x && n-- > 0) {
  116.             x = x->next;
  117.         }
  118.         return x->value;
  119.     }
  120.  
  121.     T& at(int n) {
  122.         if (n > count || n < 0)
  123.             throw std::out_of_range("Deque index is out of bounds");
  124.  
  125.         Node* x = first;
  126.         while (x && n-- > 0) {
  127.             x = x->next;
  128.         }
  129.         return x->value;
  130.     }
  131.  
  132.     const T& at(int n) const {
  133.         if (n > count || n < 0)
  134.             throw std::out_of_range("Deque index is out of bounds");
  135.  
  136.         Node* x = first;
  137.         while (x && --n) {
  138.             x = x->next;
  139.         }
  140.         return x->value;
  141.     }
  142.  
  143.     T& front() {
  144.         return first->value;
  145.     }
  146.  
  147.     const T& front() const {
  148.         return first->value;
  149.     }
  150.  
  151.     T& back() {
  152.         return last->value;
  153.     }
  154.  
  155.     const T& back() const {
  156.         return last->value;
  157.     }
  158.  
  159.     // modifiers:
  160.     void push_front(const T& x) {
  161.         if (last)
  162.             last = last->next = new Node(x, last, nullptr);
  163.         else
  164.             last = first = new Node(x, nullptr, nullptr);
  165.         count++;
  166.     }
  167.  
  168.     void push_front(T && x) {
  169.         if (last)
  170.             last = last->next = new Node(x, last, nullptr);
  171.         else
  172.             last = first = new Node(x, nullptr, nullptr);
  173.         count++;
  174.     }
  175.  
  176.     void push_back(const T& x) {
  177.         if (first)
  178.             first = first->prev = new Node(x, nullptr, first);
  179.         else
  180.             first = last = new Node(x, nullptr, nullptr);
  181.         count++;
  182.     }
  183.  
  184.     void push_back(T && x) {
  185.         if (first)
  186.             first = first->prev = new Node(x, nullptr, first);
  187.         else
  188.             first = last = new Node(x, nullptr, nullptr);
  189.         count++;
  190.     }
  191.  
  192.     void pop_back() {
  193.         if (!first)
  194.             return;
  195.         Node* temp = first;
  196.         first = first->next;
  197.         if (--count) {
  198.             first->prev = nullptr;
  199.         } else {
  200.             last = nullptr;
  201.         }
  202.         delete temp;
  203.     }
  204.  
  205.     void pop_front() {
  206.         if (!last)
  207.             return;
  208.         Node* temp = last;
  209.         last = last->prev;
  210.         if (--count) {
  211.             last->next = nullptr;
  212.         } else {
  213.             first = nullptr;
  214.         }
  215.         delete temp;
  216.     }
  217.  
  218.     void swap(ListDeque& x) {
  219.         std::swap(first, x.first);
  220.         std::swap(last, x.last);
  221.         std::swap(count, x.count);
  222.     }
  223.  
  224.     friend void swap(ListDeque& x, ListDeque& y) {
  225.         x.swap(y);
  226.     }
  227.  
  228.     void clear() {
  229.         if (!first)
  230.             return;
  231.         for (Node * n = first->next; n; n = n->next) {
  232.             delete n->prev;
  233.         }
  234.         delete last;
  235.         first = nullptr;
  236.         last = nullptr;
  237.         count = 0;
  238.     }
  239.  
  240.     // operators
  241.     bool operator == (const ListDeque& x) const {
  242.         if (count != x.count)
  243.             return false;
  244.         Node* n = first, *n2 = x.first;
  245.         while (n && n->value == n2->value) {
  246.             n = n->next; n2 = n2->next;
  247.         }
  248.         return !n && !n2;
  249.     }
  250.  
  251.     bool operator != (const ListDeque& x) const {
  252.         return !(*this == x);
  253.     }
  254.  
  255. };
  256.  
  257. // ---------------------------------------------------------------------------
  258. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement