Stack added + found bugs squashed

This commit is contained in:
2019-05-07 19:43:31 +02:00
parent 3edd654f60
commit c1e498dfa0
6 changed files with 99 additions and 6 deletions

46
sequential/stack.hpp Normal file
View File

@@ -0,0 +1,46 @@
#pragma once
#include "vector.hpp"
template<class T>
class Stack
{
private:
Vector<T> data;
std::size_t actualSize;
public:
void Push(T const & value)
{
++actualSize;
if(actualSize > data.GetSize())
{
data.Resize(actualSize);
}
data[actualSize - 1ul] = value;
}
T Pop()
{
if(actualSize == 0ul)
{
throw std::out_of_range("Cannot pop an empty stack.");
}
auto const retval = data[actualSize - 1ul];
--actualSize;
data.Resize(actualSize);
return retval;
}
std::size_t GetSize() const
{
return actualSize;
}
Stack()
: actualSize(0)
{
}
};