Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- using T = int;
- class List;
- struct Element {
- int v;
- Element * previous;
- Element * next;
- Element(const int& v, Element* next = nullptr, Element* previous = nullptr)
- : v(v), next(next), previous(previous) {
- if (next != nullptr) {
- next->previous = this;
- }
- if (previous != nullptr) {
- previous->next = this;
- }
- }
- ~Element() {
- if (previous != nullptr) {
- previous->next = nullptr;
- }
- if (next != nullptr) {
- next->previous = nullptr;
- }
- }
- };
- class Iterator {
- private:
- Element* current;
- const List& l;
- public:
- Iterator(Element* current1, const List& l1) : l(l1) {
- current = current1;
- }
- Iterator& operator++() {
- current = (*current).previous;
- return *this;
- }
- Iterator operator++(int) {
- Iterator temp = *this;
- ++(*this);
- return temp;
- }
- Iterator& operator--() {
- if (current != nullptr) {
- current = (*current).next;
- } else {
- current = l.Tail();
- }
- return *this;
- }
- Iterator operator--(int) {
- Iterator temp = *this;
- --(*this);
- return temp;
- }
- bool operator==(const Iterator& other) const {
- if (current == other.current) {
- return true;
- }
- return false;
- }
- bool operator!=(const Iterator& other) const {
- if (*this == other) {
- return false;
- }
- return true;
- }
- const int& operator*() const {
- return (*current).v;
- }
- };
- class List {
- public:
- size_t ListSize;
- Element* head;
- Element* tail;
- List() {
- ListSize = 0;
- head = nullptr;
- tail = nullptr;
- }
- List(const List& other) {
- ListSize = 0;
- head = nullptr;
- tail = nullptr;
- for (const auto& v : other) {
- ++ListSize;
- Element* temp = new Element(v, tail);
- tail = temp;
- if (head == nullptr) {
- head = temp;
- }
- }
- }
- List& operator=(const List& other) {
- while (head != nullptr) {
- Element* temp = head;
- head = (*head).previous;
- delete temp;
- }
- ListSize = 0;
- tail = nullptr;
- List temp(other);
- Element * h = head;
- head = temp.head;
- temp.head = h;
- Element * t = tail;
- tail = temp.tail;
- temp.tail = t;
- size_t s = ListSize;
- ListSize = temp.ListSize;
- temp.ListSize = s;
- return *this;
- }
- size_t size() {
- return ListSize;
- }
- void push_back(const int& v) {
- Element * el = new Element(v, tail);
- ++ListSize;
- if (ListSize == 1) {
- head = el;
- }
- tail = el;
- }
- Element* Tail() const {
- return tail;
- }
- void pop_back() {
- Element * FormerTail = tail;
- tail = (*tail).next;
- delete FormerTail;
- --ListSize;
- if (ListSize == 0)
- head = tail;
- }
- void push_front(const int& v) {
- Element * el = new Element(v, nullptr, head);
- ++ListSize;
- if (ListSize == 1) {
- tail = el;
- }
- head = el;
- }
- void pop_front() {
- Element * FormerHead = head;
- head = (*head).previous;
- delete FormerHead;
- --ListSize;
- if (ListSize == 0)
- tail = head;
- }
- Iterator begin() const {
- return Iterator(head, *this);
- }
- Iterator end() const {
- return Iterator(nullptr, *this);
- }
- ~List() {
- while (head != nullptr) {
- Element * temp = head;
- head = (*head).previous;
- delete temp;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement