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)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,7 @@ CFLAGS = -std=c++17 -Wall -g
|
||||
CPPS = $(wildcard *.cpp)
|
||||
EXES = $(patsubst %.cpp, %.out, $(CPPS))
|
||||
|
||||
.PHONY: all clean rebuild
|
||||
.PHONY: all clean rebuild check
|
||||
|
||||
%.out: %.cpp
|
||||
$(CC) $(CFLAGS) $< -o $@
|
||||
@@ -16,3 +16,8 @@ clean:
|
||||
-rm *.out
|
||||
|
||||
rebuild: clean all
|
||||
|
||||
EXESQUOTED = $(patsubst %, "%", $(EXES))
|
||||
|
||||
check: all
|
||||
./execute-all.sh
|
||||
35
test/stack.cpp
Normal file
35
test/stack.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "../sequential/stack.hpp"
|
||||
#include "testutil.hpp"
|
||||
#include <stack>
|
||||
|
||||
bool TestStack()
|
||||
{
|
||||
std::size_t testSize = 512ul;
|
||||
|
||||
std::stack<unsigned> truth;
|
||||
Stack<unsigned> stack;
|
||||
|
||||
for(std::size_t i = 0; i < testSize; ++i)
|
||||
{
|
||||
unsigned const toInsert = Util::GetRandomNumber();
|
||||
truth.push(toInsert);
|
||||
stack.Push(toInsert);
|
||||
}
|
||||
|
||||
while(!truth.empty())
|
||||
{
|
||||
if(truth.top() != stack.Pop())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
truth.pop();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Test::Execute(TestStack, "Pop push test");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user