From 1100ac3b56753682aa231cfaaf488ae8ac8194f0 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Mon, 12 Apr 2021 22:04:22 +0200 Subject: [PATCH] Library 2 implemented stacklist --- librerie/exercise2/list/list.cpp | 2 +- librerie/exercise2/stack/lst/stacklst.cpp | 80 ++++++++++++++++++++++- librerie/exercise2/stack/lst/stacklst.hpp | 41 ++++++------ librerie/exercise2/stack/stack.hpp | 26 ++++---- librerie/exercise2/stack/vec/stackvec.hpp | 48 +++++++------- 5 files changed, 135 insertions(+), 62 deletions(-) diff --git a/librerie/exercise2/list/list.cpp b/librerie/exercise2/list/list.cpp index 371af2d..7f5b3b4 100644 --- a/librerie/exercise2/list/list.cpp +++ b/librerie/exercise2/list/list.cpp @@ -129,7 +129,7 @@ bool List::operator!=(const List& list) const noexcept{ void List::InsertAtFront(Data&& data){ struct Node* tmp = new Node(data); tmp->next = head; - head =tmp; + head=tmp; size++; if(size == 1){ diff --git a/librerie/exercise2/stack/lst/stacklst.cpp b/librerie/exercise2/stack/lst/stacklst.cpp index d374ceb..0bb6885 100755 --- a/librerie/exercise2/stack/lst/stacklst.cpp +++ b/librerie/exercise2/stack/lst/stacklst.cpp @@ -3,7 +3,85 @@ namespace lasd { /* ************************************************************************** */ -// ... +// Constructors +template +StackLst(const LinearContainer& linear){ + for(ulong i=linear.Size()-1 ; i>=0 ; --i){ // รจ possibile usare il costruttore di List? + Push(linear[i]); + } +} + +StackLst(const StackLst& stcklist){ + for(ulong i=stcklist.Size()-1 ; i>=0 ; --i){ + Push(stcklist[i]); + } +} + +template +StackLst::StackLst(StackLst&& stcklist) noexcept{ + std::swap(size, stcklist.size); + std::swap(head, stcklist.head); +} + +// Destructor +virtual ~StackLst(){ + Clear(); +} + +StackLst& operator=(const StackLst& copyFrom){ + if(*this != copyFrom){ + Clear(); + for(ulong i=copyFrom.Size()-1 ; i>=0 ; --i){ + Push(copyFrom[i]); + } + } + return *this; +} + +StackLst& operator=(StackLst&& moveFrom){ + std::swap(size, moveFrom.size); + std::swap(head, moveFrom.head); + return *this; +} + +bool operator==(const StackLst& stcklist) const noexcept{ + return List::operator==(stcklist); +} + +bool operator!=(const StackLst& stcklist) const noexcept{ + return List::operator!=(stcklist); +} + +// Specific member functions (inherited from Stack) +template +void Push(const Data& element){ + List::InsertAtFront(element); +} + +template +void Push(Data&& element){ + List::InsertAtFront(element); +} + +template +Data& Top() const{ + return List::Front(); +} + +template +void Pop(){ + List::RemoveFromFront(); +} + +template +Data TopNPop(){ + return List::FrontNRemove(); +} + +template +void Clear(){ + List::Clear(); +} /* ************************************************************************** */ diff --git a/librerie/exercise2/stack/lst/stacklst.hpp b/librerie/exercise2/stack/lst/stacklst.hpp index 67fbb32..16ce8fb 100755 --- a/librerie/exercise2/stack/lst/stacklst.hpp +++ b/librerie/exercise2/stack/lst/stacklst.hpp @@ -14,70 +14,69 @@ namespace lasd { /* ************************************************************************** */ template -class StackLst { // Must extend Stack and List +class StackLst : virtual public Stack, + virtual protected List { // Must extend Stack and List private: - // ... - protected: - // using List::???; - - // ... + using List::head; + using List::size; + using typename List::Node; public: // Default constructor - // StackLst() specifier; + StackLst() = default; /* ************************************************************************ */ // Specific constructor - // StackLst(argument) specifiers; // A stack obtained from a LinearContainer + StackLst(const LinearContainer&); // A stack obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // StackLst(argument); + StackLst(const StackLst&); // Move constructor - // StackLst(argument); + StackLst(StackLst&&) noexcept; /* ************************************************************************ */ // Destructor - // ~StackLst() specifier; + virtual ~StackLst(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + StackLst& operator=(const StackLst&); // Move assignment - // type operator=(argument); + StackLst& operator=(StackLst&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const StackLst&) const noexcept; + bool operator!=(const StackLst&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Stack) - // type Push(argument) specifiers; // Override Stack member (copy of the value) - // type Push(argument) specifiers; // Override Stack member (move of the value) - // type Top() specifiers; // Override Stack member (must throw std::length_error when empty) - // type Pop() specifiers; // Override Stack member (must throw std::length_error when empty) - // type TopNPop() specifiers; // Override Stack member (must throw std::length_error when empty) + void Push(const Data&) override; // Override Stack member (copy of the value) + void Push(Data&&) noexcept override; // Override Stack member (move of the value) + Data& Top() const override; // Override Stack member (must throw std::length_error when empty) + void Pop() override; // Override Stack member (must throw std::length_error when empty) + Data TopNPop() override; // Override Stack member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Clear() specifiers; // Override Container member + void Clear() override; // Override Container member }; diff --git a/librerie/exercise2/stack/stack.hpp b/librerie/exercise2/stack/stack.hpp index 73a3a7c..fee854b 100755 --- a/librerie/exercise2/stack/stack.hpp +++ b/librerie/exercise2/stack/stack.hpp @@ -13,44 +13,40 @@ namespace lasd { /* ************************************************************************** */ template -class Stack { // Must extend Container +class Stack : virtual public Container { // Must extend Container private: - // ... - protected: - // ... - public: // Destructor - // ~Stack() specifiers + virtual ~Stack() = default; /* ************************************************************************ */ // Copy assignment - // type operator=(argument); // Copy assignment of abstract types should not be possible. + Stack& operator=(const Stack&) = delete; // Copy assignment of abstract types should not be possible. // Move assignment - // type operator=(argument); // Move assignment of abstract types should not be possible. + Stack&operator=(Stack&&) = delete; // Move assignment of abstract types should not be possible. /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; // Comparison of abstract types might not be possible. - // type operator!=(argument) specifiers; // Comparison of abstract types might not be possible. + bool operator==(const Stack&) = delete; // Comparison of abstract types might not be possible. + bool operator!=(Stack&&) = delete; // Comparison of abstract types might not be possible. /* ************************************************************************ */ // Specific member functions - // type Push(argument) specifiers; // Copy of the value - // type Push(argument) specifiers; // Move of the value - // type Top() specifiers; // (concrete function must throw std::length_error when empty) - // type Pop() specifiers; // (concrete function must throw std::length_error when empty) - // type TopNPop() specifiers; // (concrete function must throw std::length_error when empty) + virtual void Push(const Data&) = 0; // Copy of the value + virtual void Push(Data&&) = 0; // Move of the value + virtual Data& Top() const = 0; // (concrete function must throw std::length_error when empty) + virtual void Pop() = 0; // (concrete function must throw std::length_error when empty) + virtual Data TopNPop() = 0; // (concrete function must throw std::length_error when empty) }; diff --git a/librerie/exercise2/stack/vec/stackvec.hpp b/librerie/exercise2/stack/vec/stackvec.hpp index 5e65367..b4211dc 100755 --- a/librerie/exercise2/stack/vec/stackvec.hpp +++ b/librerie/exercise2/stack/vec/stackvec.hpp @@ -14,81 +14,81 @@ namespace lasd { /* ************************************************************************** */ template -class StackVec { // Must extend Stack and Vector +class StackVec : virtual public Stack, + virtual protected Vector{ // Must extend Stack and Vector private: - // ... - protected: - // using Vector::???; + ulong stackSize = 0; // Actual stack dimension + using Vector::Elements; + using Vector::size; - // ... public: // Default constructor - // StackVec() specifier; + StackVec(); /* ************************************************************************ */ // Specific constructor - // StackVec(argument) specifiers; // A stack obtained from a LinearContainer + StackVec(const LinearContainer&); // A stack obtained from a LinearContainer /* ************************************************************************ */ // Copy constructor - // StackVec(argument); + StackVec(const StackVec&); // Move constructor - // StackVec(argument); + StackVec(StackVec&&) noexcept; /* ************************************************************************ */ // Destructor - // ~StackVec() specifier; + virtual ~StackVec(); /* ************************************************************************ */ // Copy assignment - // type operator=(argument); + StackVec& operator=(const StackVec&) // Move assignment - // type operator=(argument); + StackVec& operator=(StackVec&&) noexcept; /* ************************************************************************ */ // Comparison operators - // type operator==(argument) specifiers; - // type operator!=(argument) specifiers; + bool operator==(const StackVec&) const noexcept; + bool operator!=(const StackVec&) const noexcept; /* ************************************************************************ */ // Specific member functions (inherited from Stack) - // type Push(argument) specifiers; // Override Stack member (copy of the value) - // type Push(argument) specifiers; // Override Stack member (move of the value) - // type Top() specifiers; // Override Stack member (must throw std::length_error when empty) - // type Pop() specifiers; // Override Stack member (must throw std::length_error when empty) - // type TopNPop() specifiers; // Override Stack member (must throw std::length_error when empty) + void Push(const Data&) override; // Override Stack member (copy of the value) + void Push(Data&&) override; // Override Stack member (move of the value) + Data& Top() const override; // Override Stack member (must throw std::length_error when empty) + void Pop() override; // Override Stack member (must throw std::length_error when empty) + Data TopNPop() override; // Override Stack member (must throw std::length_error when empty) /* ************************************************************************ */ // Specific member functions (inherited from Container) - // type Empty() specifiers; // Override Container member + bool Empty() const noexcept override; // Override Container member - // type Size() specifiers; // Override Container member + ulong Size() const noexcept override; specifiers; // Override Container member - // type Clear() specifiers; // Override Container member + void Clear() override;// Override Container member protected: // Auxiliary member functions - // type Expand() specifiers; - // type Reduce() specifiers; + void Expand(); + void Reduce(); };