Stack added + found bugs squashed
This commit is contained in:
@@ -26,7 +26,7 @@ private:
|
||||
std::size_t size;
|
||||
|
||||
public:
|
||||
void Append(T const value)
|
||||
void Append(T const & value)
|
||||
{
|
||||
if(size == 0)
|
||||
{
|
||||
@@ -42,7 +42,7 @@ public:
|
||||
++size;
|
||||
}
|
||||
|
||||
void Prepend(T const value)
|
||||
void Prepend(T const & value)
|
||||
{
|
||||
if(size == 0)
|
||||
{
|
||||
@@ -59,7 +59,7 @@ public:
|
||||
++size;
|
||||
}
|
||||
|
||||
void Insert(T const value, std::size_t const index)
|
||||
void Insert(T const & value, std::size_t const index)
|
||||
{
|
||||
if(index >= size)
|
||||
{
|
||||
|
||||
@@ -31,7 +31,7 @@ private:
|
||||
}
|
||||
|
||||
public:
|
||||
void Push(const T value)
|
||||
void Push(T const & value)
|
||||
{
|
||||
data[tail] = value;
|
||||
AdvanceTail();
|
||||
|
||||
46
sequential/stack.hpp
Normal file
46
sequential/stack.hpp
Normal 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)
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -12,6 +12,13 @@ private:
|
||||
public:
|
||||
void Resize(std::size_t const newSize)
|
||||
{
|
||||
if(newSize == 0ul)
|
||||
{
|
||||
std::free(data);
|
||||
data = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
void * result = std::realloc(data, newSize * sizeof(T));
|
||||
if(result == nullptr)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user