Library 2

implemented stacklist
This commit is contained in:
Alessandro Ferro 2021-04-12 22:04:22 +02:00
parent c30aaf96c9
commit 1100ac3b56
5 changed files with 135 additions and 62 deletions

View File

@ -129,7 +129,7 @@ bool List<Data>::operator!=(const List<Data>& list) const noexcept{
void List<Data>::InsertAtFront(Data&& data){
struct Node* tmp = new Node(data);
tmp->next = head;
head =tmp;
head=tmp;
size++;
if(size == 1){

View File

@ -3,7 +3,85 @@ namespace lasd {
/* ************************************************************************** */
// ...
// Constructors
template <typename Data>
StackLst(const LinearContainer<Data>& 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 <typename Data>
StackLst<Data>::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<Data>::operator==(stcklist);
}
bool operator!=(const StackLst& stcklist) const noexcept{
return List<Data>::operator!=(stcklist);
}
// Specific member functions (inherited from Stack)
template <typename Data>
void Push(const Data& element){
List<Data>::InsertAtFront(element);
}
template <typename Data>
void Push(Data&& element){
List<Data>::InsertAtFront(element);
}
template <typename Data>
Data& Top() const{
return List<Data>::Front();
}
template <typename Data>
void Pop(){
List<Data>::RemoveFromFront();
}
template <typename Data>
Data TopNPop(){
return List<Data>::FrontNRemove();
}
template <typename Data>
void Clear(){
List<Data>::Clear();
}
/* ************************************************************************** */

View File

@ -14,70 +14,69 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class StackLst { // Must extend Stack<Data> and List<Data>
class StackLst : virtual public Stack<Data>,
virtual protected List<Data> { // Must extend Stack<Data> and List<Data>
private:
// ...
protected:
// using List<Data>::???;
// ...
using List<Data>::head;
using List<Data>::size;
using typename List<Data>::Node;
public:
// Default constructor
// StackLst() specifier;
StackLst() = default;
/* ************************************************************************ */
// Specific constructor
// StackLst(argument) specifiers; // A stack obtained from a LinearContainer
StackLst(const LinearContainer<Data>&); // 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
};

View File

@ -13,44 +13,40 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
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)
};

View File

@ -14,81 +14,81 @@ namespace lasd {
/* ************************************************************************** */
template <typename Data>
class StackVec { // Must extend Stack<Data> and Vector<Data>
class StackVec : virtual public Stack<Data>,
virtual protected Vector<Data>{ // Must extend Stack<Data> and Vector<Data>
private:
// ...
protected:
// using Vector<Data>::???;
ulong stackSize = 0; // Actual stack dimension
using Vector<Data>::Elements;
using Vector<Data>::size;
// ...
public:
// Default constructor
// StackVec() specifier;
StackVec();
/* ************************************************************************ */
// Specific constructor
// StackVec(argument) specifiers; // A stack obtained from a LinearContainer
StackVec(const LinearContainer<Data>&); // 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();
};