#include "List.h" #include #include #include struct List::Node { Node() = default; Node(int v, Node* p, Node* n) : value{v}, prev{p}, next{n} {} int value {}; Node * prev {}; Node * next {}; }; List::List() : head{ new Node{} }, tail{head}, sz{} {} List::List(List const & other) : List{} { Node * tmp = other.head; while ( tmp != other.tail ) { push_back(tmp->value); tmp = tmp->next; } } List::List(List && tmp) noexcept :List{} { swap(tmp); } List::List(std::initializer_list lst) : List{} { for ( auto val : lst ) { push_back(val); } } void List::push_front(int value) { head = new Node{value, nullptr, head}; if ( sz == 0 ) { tail->prev = head; } ++sz; } void List::push_back(int value) { if ( empty() ) { push_front(value); } else { tail->prev->next = new Node{value, tail->prev, tail}; tail->prev = tail->prev->next; ++sz; } } bool List::empty() const { return head == tail; } int & List::at(int idx) { if (idx >= sz) throw std::out_of_range{"Index not found"}; Node * tmp {head}; while ( idx > 0 ) { tmp = tmp->next; --idx; } return tmp->value; } int List::size() const { return sz; } void List::swap(List & other) noexcept { using std::swap; swap(head, other.head); swap(tail, other.tail); swap(sz, other.sz); } List & List::operator=(List const & rhs) & { List{rhs}.swap(*this); return *this; } List & List::operator=(List && rhs) & { swap(rhs); return *this; } void List::sort() { bool swapped {true}; for ( int iteration {}; swapped && iteration < size(); ++iteration ) { swapped = false; for ( auto p = head; p != tail; p = p->next ) { if ( p->value > p->next->value ) { swap_nodes(p, p->next); swapped = true; } } } } }