#include "stack.h" #include Stack::Stack(Stack const& other) : first{clone(other.first)} { } Stack& Stack::operator=(Stack const& other) { Stack tmp {other}; std::swap(tmp.first, first); return *this; } Stack::Stack(Stack && other) : first{other.first} { other.first = nullptr; } Stack& Stack::operator=(Stack && other) { std::swap(other.first, first); return *this; } Stack::~Stack() { while(not is_empty()) { pop(); } } void Stack::push(int v) { first = new Node{v, first}; } int Stack::top() const { if (is_empty()) { throw std::out_of_range("Stack is empty"); } return first->value; } void Stack::pop() { if (is_empty()) { throw std::out_of_range("Stack is empty"); } Node* to_remove {first}; first = first->next; delete to_remove; } bool Stack::is_empty() const { return first == nullptr; } Stack::Node* Stack::clone(Stack::Node* n) { if (n == nullptr) { return nullptr; } return new Node{n->value, clone(n->next)}; }