Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "node2.h"
- #include <cassert>
- #ifndef PRAKI_LIST_H
- #define PRAKI_LIST_H
- template<typename T>
- class mList {
- public:
- long long size = 0, lastIndex = 0;
- bool empty = true;
- private:
- node2<T> *frontField = nullptr, *backField = nullptr, *lastNode = nullptr;
- public:
- void pushBack(T val, bool checkAvailability = false) {
- if (checkAvailability and contain(val)) return;
- ++size;
- empty = false;
- auto *newNode = new node2<T>(val);
- if (size == 1) {
- backField = newNode;
- frontField = newNode;
- } else {
- backField->last = newNode;
- newNode->next = backField;
- backField = newNode;
- }
- }
- void popBack() {
- if (empty) return;
- --size;
- if (size == 0) {
- delete backField;
- backField = frontField = nullptr;
- } else if (size == 1) {
- delete backField;
- backField = frontField;
- frontField->next = nullptr;
- } else {
- node2<T> *t = backField;
- backField = backField->next;
- delete t;
- }
- }
- void pop() {
- if (empty) return;
- --size;
- empty = size == 0;
- if (size == 0) {
- delete frontField;
- frontField = backField = nullptr;
- } else if (size == 1) {
- delete frontField;
- frontField = backField;
- backField->last = nullptr;
- } else {
- node2<T> *t = frontField;
- frontField = frontField->last;
- delete t;
- }
- }
- bool isEmpty() {
- return size == 0;
- }
- T front() {
- if (!empty) return frontField->elem;
- return T();
- }
- T back() {
- if (!empty) return backField->elem;
- return T();
- }
- node2<T> *begin() { return frontField; }
- node2<T> *end() { return backField; }
- void clear() {
- node2<T> *t;
- lastIndex = 0;
- lastNode = nullptr;
- for (int i = 0; i < size; ++i) {
- t = frontField;
- frontField = frontField->last;
- delete t;
- }
- size = 0;
- empty = true;
- }
- T &operator[](const long long index) {
- assert(this->size > index >= 0);
- node2<T> *current = (lastNode) ? lastNode : frontField;
- if (index > lastIndex) {
- for (int i = lastIndex; i < index; ++i) {
- current = current->last;
- }
- } else if (index < lastIndex) {
- for (int i = lastIndex; i > index; --i) {
- current = current->next;
- }
- }
- lastNode = current;
- lastIndex = index;
- return current->elem;
- };
- // T &operator=(const T val) {
- // if (size > 0) lastNode->elem = val;
- // cout << "ты гей" << endl;
- // return *this;
- // }
- mList() = default;
- explicit mList<T>(long long s) {
- for (long long i = 0; i < s; ++i) pushBack(T());
- }
- mList<T>(long long s, T val) {
- for (long long i = 0; i < s; ++i) pushBack(val);
- }
- mList<T>(const std::initializer_list<T> &data) {
- for (T element : data) {
- pushBack(element);
- }
- }
- int indexOf(T val) {
- int ret = 0;
- for (node2<T> *i = frontField; i != nullptr; i = i->last) {
- if (i->elem == val) return ret;
- ++ret;
- }
- return -1;
- }
- bool contain(T val) {
- for (node2<T> *i = frontField; i != nullptr; i = i->last)
- if (i->elem == val) return true;
- return false;
- }
- T eraseAt(int id) {
- assert(0 <= id < this->size);
- }
- ~mList<T>() {
- clear();
- delete frontField, backField, lastNode;
- }
- };
- #endif //PRAKI_LIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement