Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include "node2.h"
  2. #include <cassert>
  3.  
  4. #ifndef PRAKI_LIST_H
  5. #define PRAKI_LIST_H
  6.  
  7. template<typename T>
  8. class mList {
  9.   public:
  10.   long long size = 0, lastIndex = 0;
  11.   bool empty = true;
  12.   private:
  13.   node2<T> *frontField = nullptr, *backField = nullptr, *lastNode = nullptr;
  14.  
  15.   public:
  16.   void pushBack(T val, bool checkAvailability = false) {
  17.     if (checkAvailability and contain(val)) return;
  18.     ++size;
  19.     empty = false;
  20.     auto *newNode = new node2<T>(val);
  21.     if (size == 1) {
  22.       backField = newNode;
  23.       frontField = newNode;
  24.     } else {
  25.       backField->last = newNode;
  26.       newNode->next = backField;
  27.       backField = newNode;
  28.     }
  29.   }
  30.  
  31.   void popBack() {
  32.     if (empty) return;
  33.     --size;
  34.     if (size == 0) {
  35.       delete backField;
  36.       backField = frontField = nullptr;
  37.     } else if (size == 1) {
  38.       delete backField;
  39.       backField = frontField;
  40.       frontField->next = nullptr;
  41.     } else {
  42.       node2<T> *t = backField;
  43.       backField = backField->next;
  44.       delete t;
  45.     }
  46.   }
  47.  
  48.   void pop() {
  49.     if (empty) return;
  50.     --size;
  51.     empty = size == 0;
  52.     if (size == 0) {
  53.       delete frontField;
  54.       frontField = backField = nullptr;
  55.     } else if (size == 1) {
  56.       delete frontField;
  57.       frontField = backField;
  58.       backField->last = nullptr;
  59.     } else {
  60.       node2<T> *t = frontField;
  61.       frontField = frontField->last;
  62.       delete t;
  63.     }
  64.   }
  65.  
  66.   bool isEmpty() {
  67.     return size == 0;
  68.   }
  69.  
  70.   T front() {
  71.     if (!empty) return frontField->elem;
  72.     return T();
  73.   }
  74.  
  75.   T back() {
  76.     if (!empty) return backField->elem;
  77.     return T();
  78.   }
  79.  
  80.   node2<T> *begin() { return frontField; }
  81.  
  82.   node2<T> *end() { return backField; }
  83.  
  84.   void clear() {
  85.     node2<T> *t;
  86.     lastIndex = 0;
  87.     lastNode = nullptr;
  88.     for (int i = 0; i < size; ++i) {
  89.       t = frontField;
  90.       frontField = frontField->last;
  91.       delete t;
  92.     }
  93.     size = 0;
  94.     empty = true;
  95.   }
  96.  
  97.   T &operator[](const long long index) {
  98.     assert(this->size > index >= 0);
  99.     node2<T> *current = (lastNode) ? lastNode : frontField;
  100.     if (index > lastIndex) {
  101.       for (int i = lastIndex; i < index; ++i) {
  102.         current = current->last;
  103.       }
  104.     } else if (index < lastIndex) {
  105.       for (int i = lastIndex; i > index; --i) {
  106.         current = current->next;
  107.       }
  108.     }
  109.     lastNode = current;
  110.     lastIndex = index;
  111.     return current->elem;
  112.   };
  113.  
  114. //  T &operator=(const T val) {
  115. //    if (size > 0) lastNode->elem = val;
  116. //    cout << "ты гей" << endl;
  117. //    return *this;
  118. //  }
  119.  
  120.   mList() = default;
  121.  
  122.   explicit mList<T>(long long s) {
  123.     for (long long i = 0; i < s; ++i) pushBack(T());
  124.   }
  125.  
  126.   mList<T>(long long s, T val) {
  127.     for (long long i = 0; i < s; ++i) pushBack(val);
  128.   }
  129.  
  130.   mList<T>(const std::initializer_list<T> &data) {
  131.     for (T element : data) {
  132.       pushBack(element);
  133.     }
  134.   }
  135.  
  136.   int indexOf(T val) {
  137.     int ret = 0;
  138.     for (node2<T> *i = frontField; i != nullptr; i = i->last) {
  139.       if (i->elem == val) return ret;
  140.       ++ret;
  141.     }
  142.     return -1;
  143.   }
  144.  
  145.   bool contain(T val) {
  146.     for (node2<T> *i = frontField; i != nullptr; i = i->last)
  147.       if (i->elem == val) return true;
  148.     return false;
  149.   }
  150.  
  151.   T eraseAt(int id) {
  152.     assert(0 <= id < this->size);
  153.   }
  154.  
  155.   ~mList<T>() {
  156.     clear();
  157.     delete frontField, backField, lastNode;
  158.   }
  159. };
  160.  
  161. #endif //PRAKI_LIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement