diff --git a/sequential/linkedlist.hpp b/sequential/linkedlist.hpp index 5b8775e..330d741 100644 --- a/sequential/linkedlist.hpp +++ b/sequential/linkedlist.hpp @@ -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) { diff --git a/sequential/ringbuffer.hpp b/sequential/ringbuffer.hpp index 6d835ea..1039042 100644 --- a/sequential/ringbuffer.hpp +++ b/sequential/ringbuffer.hpp @@ -31,7 +31,7 @@ private: } public: - void Push(const T value) + void Push(T const & value) { data[tail] = value; AdvanceTail(); diff --git a/sequential/stack.hpp b/sequential/stack.hpp new file mode 100644 index 0000000..073af24 --- /dev/null +++ b/sequential/stack.hpp @@ -0,0 +1,46 @@ +#pragma once +#include "vector.hpp" + +template +class Stack +{ +private: + Vector 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) + { + } +}; \ No newline at end of file diff --git a/sequential/vector.hpp b/sequential/vector.hpp index 53015b5..688b1c4 100644 --- a/sequential/vector.hpp +++ b/sequential/vector.hpp @@ -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) { diff --git a/test/makefile b/test/makefile index 1b27290..2c039b9 100644 --- a/test/makefile +++ b/test/makefile @@ -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 $@ @@ -15,4 +15,9 @@ clean: -rm *.o -rm *.out -rebuild: clean all \ No newline at end of file +rebuild: clean all + +EXESQUOTED = $(patsubst %, "%", $(EXES)) + +check: all + ./execute-all.sh \ No newline at end of file diff --git a/test/stack.cpp b/test/stack.cpp new file mode 100644 index 0000000..b8a12a2 --- /dev/null +++ b/test/stack.cpp @@ -0,0 +1,35 @@ +#include "../sequential/stack.hpp" +#include "testutil.hpp" +#include + +bool TestStack() +{ + std::size_t testSize = 512ul; + + std::stack truth; + Stack 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; +} \ No newline at end of file