Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4. #include <algorithm>
  5. #include <iomanip>
  6. #include <math.h>
  7. #include <set>
  8. #include <utility>
  9. #include <map>
  10. #include <string>
  11. #include <exception>
  12. #include <stddef.h>
  13. #include <memory>
  14. #include <list>
  15.  
  16. using namespace std;
  17.  
  18. template <typename T>
  19. class Value;
  20.  
  21. template<typename T>
  22. class LIterator {
  23. public:
  24.     Value<T>* cur;
  25.     LIterator<T>() {
  26.     }
  27.     LIterator<T>(Value<T> * new_cur) {
  28.         cur = new_cur;
  29.     }
  30.     LIterator<T>& operator++() {
  31.         cur = cur->next;
  32.         return (*this);
  33.     }
  34.     const T operator*() const {
  35.         return *cur->field;
  36.     }
  37.     bool operator== (const LIterator<T>& other) {
  38.         return cur == other.cur;
  39.     }
  40.     bool operator != (const LIterator<T>& other) {
  41.         return !(*this == other);
  42.     }
  43. };
  44.  
  45. template <typename T>
  46. class Value{
  47. public:
  48.     T* field = nullptr;
  49.     Value* next = nullptr;
  50.     Value* prev = nullptr;
  51.     Value<T>() {
  52. //        cout << "default constructor\n";
  53.     }
  54.     ~Value() {
  55.         delete field;
  56.     }
  57. };
  58.  
  59. template <typename T>
  60. class List{
  61. public:
  62.     Value<T>* fir;
  63.     Value<T>* last;
  64.     Value<T> fake;
  65.     int sz = 0;
  66.     List<T>() {
  67. //        cout << "List default constructor\n";
  68.         fir = &fake;
  69.         last = &fake;
  70.     }
  71.     int size() const {
  72.         return sz;
  73.     }
  74.     void push_back(const T& new_val) {
  75.         if (sz == 0) {
  76.             fir = new Value<T>;
  77.             fir->field = new T(new_val);
  78.             fir->next = last;
  79.             last->prev=  fir;
  80.             ++sz;
  81.             return;
  82.         }
  83.         ++sz;
  84.         Value<T>* cur = new Value<T>;
  85.         cur->field = new T(new_val);
  86.         cur->prev = last->prev;
  87.         cur->next = last;
  88.         last->prev->next = cur;
  89.         last->prev = cur;
  90.     }
  91.     void pop_back() {
  92.         if (sz == 1) {
  93.             delete fir;
  94.             fir = last;
  95.             fir->next = nullptr;
  96.             last->prev = nullptr;
  97.         } else {
  98.             last->prev = last->prev->prev;
  99.             delete last->prev->next;
  100.             last->prev->next = last;
  101.         }
  102.         --sz;
  103.     }
  104.     void push_front(const T& new_val) {
  105.         if (sz == 0) {
  106.             fir = new Value<T>;
  107.             fir->field = new T(new_val);
  108.             fir->next = last;
  109.             last->prev=  fir;
  110.             ++sz;
  111.             return;
  112.         }
  113.         ++sz;
  114.         Value<T>* cur = new Value<T>;
  115.         cur->field = new T(new_val);
  116.         cur->prev = nullptr;
  117.         cur->next = fir;
  118.         fir->prev = cur;
  119.         fir = cur;
  120.     }
  121.     void pop_front() {
  122.         if (sz == 1) {
  123.             delete fir;
  124.             fir = last;
  125.             fir->next = nullptr;
  126.             last->prev = nullptr;
  127.         } else {
  128.             fir = fir->next;
  129.             delete fir->prev;
  130.         }
  131.         --sz;
  132.     }
  133.     LIterator<T> begin() {
  134.         return LIterator<T>(fir);
  135.     }
  136.     LIterator<T> end() {
  137.         return LIterator<T>(last);
  138.     }
  139.     ~List() {
  140.         while (sz) {
  141.             pop_front();
  142.         }
  143.     }
  144. };
  145.  
  146. int main() {
  147.     List<int> l;
  148.     l.push_back(123);
  149.     l.pop_front();
  150.     for (auto i = l.begin(); i != l.end(); ++i) {
  151.         cout << *i << " ";
  152.     }
  153. //    Value<int> a, b;
  154. //    a.field = new int(123), b.field = new int(44);
  155. //    Value<int>* ptr = &a;
  156. //    (*ptr).next = &b;
  157. //    b.prev = &(*ptr);
  158. //    ptr = ptr->next;
  159. //    cout << *ptr->field << endl;
  160. //    ptr->next = nullptr;
  161. //    ptr->next = nullptr
  162. //    cout << *ptr->field;
  163. //    delete ptr->next;
  164. //    delete ptr->field;
  165. //    delete ptr->next;
  166. //    ptr->field = nullptr;
  167. //    ptr->next = nullptr;
  168. //    cout << (*ptr).field << endl;
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement