Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Element {
- Element *previous = nullptr;
- Element *next = nullptr;
- bool is_begin = false;
- bool is_end = false;
- int value = -991231231;
- };
- class CustomIterator {
- private:
- Element *ptr;
- public:
- explicit CustomIterator(Element *other_ptr) {
- ptr = other_ptr;
- }
- CustomIterator &operator++() {
- ptr = ptr->next;
- return *this;
- }
- CustomIterator &operator--() {
- ptr = ptr->previous;
- return *this;
- }
- bool operator==(const CustomIterator &rhs) const {
- return ptr->previous == rhs.ptr->previous &&
- ptr->next == rhs.ptr->next && ptr->value == rhs.ptr->value;
- }
- bool operator!=(const CustomIterator &rhs) const {
- return !(rhs == *this);
- }
- int operator*() {
- return ptr->value;
- }
- };
- class List {
- private:
- Element *begin_ptr;
- Element *end_ptr;
- size_t size_list = 0;
- public:
- List() {
- begin_ptr = new Element;
- begin_ptr->is_begin = true;
- end_ptr = new Element;
- end_ptr->is_end = true;
- begin_ptr->next = end_ptr;
- end_ptr->previous = begin_ptr;
- }
- List &operator=(const List &other) {
- for (auto it = other.begin(); it != other.end(); ++it) {
- push_back(*it);
- }
- return *this;
- }
- void push_back(int elem) {
- auto *temp = new Element;
- end_ptr->value = elem;
- end_ptr->next = temp;
- end_ptr->is_end = false;
- temp->is_end = true;
- temp->previous = end_ptr;
- end_ptr = temp;
- size_list++;
- }
- void push_front(int elem) {
- auto *temp = new Element;
- begin_ptr->value = elem;
- begin_ptr->previous = temp;
- begin_ptr->is_begin = false;
- temp->is_begin = true;
- temp->next = begin_ptr;
- begin_ptr = temp;
- size_list++;
- }
- void pop_back() {
- auto temp = end_ptr->previous;
- end_ptr->previous = temp->previous;
- end_ptr->previous->next = end_ptr;
- delete (temp);
- size_list--;
- }
- void pop_front() {
- auto temp = begin_ptr->next;
- begin_ptr->next = begin_ptr->next->next;
- begin_ptr->next->previous = begin_ptr;
- delete (temp);
- size_list--;
- }
- size_t size() const {
- return size_list;
- }
- CustomIterator begin() const {
- CustomIterator a(begin_ptr);
- if (size_list > 0) {
- ++a;
- }
- return a;
- }
- CustomIterator end() const {
- CustomIterator a(end_ptr);
- return a;
- }
- ~List() {
- auto act = begin_ptr;
- auto next = act->next;
- while (next != nullptr) {
- delete act;
- act = next;
- next = act->next;
- }
- delete act;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement