create class Stack. Tests passed.

This commit is contained in:
Shine wOng
2019-05-20 09:36:58 +08:00
parent 3f95c2cb62
commit 6c596c93d4
2 changed files with 54 additions and 0 deletions

15
thu_dsa/chp4/Stack.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef STACK_H_
#define STACK_H_
#include "../chp2/Vector.h"
template <typename T>
class Stack: public Vector<T>{
public:
//stack basic operations
void push(T const &e) { push_back(e); }
T pop() { return pop_back(); }
T& top() { return get(getSize() - 1); }
};
#endif

View File

@@ -0,0 +1,39 @@
#include "../chp2/Fib.h"
#include "Stack.h"
#include "time.h"
#include <iostream>
#include <cassert>
#define SIZE 1000
void test_basic();
void test_capacity();
int main(){
test_basic();
test_capacity();
system("pause");
return 0;
}
void test_basic(){
Stack<int> 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<int> stack;
srand((unsigned)time(NULL));
for (int ix = 0; ix != SIZE; ++ix)
stack.push(rand());
assert(stack.getSize() == 1000);
}