#include "list.hh" List::List() : nb_elts_{ 0 } {} void List::push_front(int i) { nb_elts_++; if (nb_elts_ == 1) { first_ = std::make_shared(i); last_ = first_; return; } std::shared_ptr ns = std::make_shared(i); ns->set_next(first_); first_->set_prev(ns); first_ = ns; } std::optional List::pop_front() { if (nb_elts_ == 0) { return std::nullopt; } nb_elts_--; int res = first_->get_val(); if (nb_elts_ == 0) { first_ = nullptr; last_ = nullptr; return res; } std::shared_ptr tmp = first_->get_next(); first_->set_next(nullptr); tmp->set_prev(nullptr); first_ = tmp; return res; } void List::push_back(int i) { nb_elts_++; if (nb_elts_ == 1) { first_ = std::make_shared(i); last_ = first_; return; } std::shared_ptr ns = std::make_shared(i); ns->set_prev(last_); last_->set_next(ns); last_ = ns; } std::optional List::pop_back() { if (nb_elts_ == 0) { return std::nullopt; } nb_elts_--; int res = last_->get_val(); if (nb_elts_ == 0) { first_ = nullptr; last_ = nullptr; return res; } std::shared_ptr tmp = last_->get_prev(); last_->set_prev(nullptr); tmp->set_next(nullptr); last_ = tmp; return res; } void List::print(std::ostream& os) const { if (nb_elts_ == 0) return; os << first_->get_val(); for (Node* node = first_->get_next().get(); node; node = node->get_next().get()) { os << " " << node->get_val(); } // os << "\n"; } size_t List::length() const { return nb_elts_; }