diff --git a/thu_dsa/chp4/Stack.h b/thu_dsa/chp4/Stack.h new file mode 100644 index 0000000..09e6b8f --- /dev/null +++ b/thu_dsa/chp4/Stack.h @@ -0,0 +1,15 @@ +#ifndef STACK_H_ +#define STACK_H_ + +#include "../chp2/Vector.h" + +template +class Stack: public Vector{ +public: + //stack basic operations + void push(T const &e) { push_back(e); } + T pop() { return pop_back(); } + T& top() { return get(getSize() - 1); } +}; + +#endif diff --git a/thu_dsa/chp4/testStack.cpp b/thu_dsa/chp4/testStack.cpp new file mode 100644 index 0000000..6f70756 --- /dev/null +++ b/thu_dsa/chp4/testStack.cpp @@ -0,0 +1,39 @@ +#include "../chp2/Fib.h" +#include "Stack.h" +#include "time.h" +#include +#include + +#define SIZE 1000 + +void test_basic(); +void test_capacity(); + +int main(){ + test_basic(); + test_capacity(); + + system("pause"); + return 0; +} + +void test_basic(){ + Stack stack; + stack.push(2); + stack.push(7); + assert(stack.getSize() == 2); + assert(stack.top() == 7); + assert(stack.pop() == 7); + assert(stack.getSize() == 1); + assert(stack.top() == 2); + assert(stack.pop() == 2); + assert(stack.getSize() == 0); +} + +void test_capacity(){ + Stack stack; + srand((unsigned)time(NULL)); + for (int ix = 0; ix != SIZE; ++ix) + stack.push(rand()); + assert(stack.getSize() == 1000); +}