-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathArrayStack.h
65 lines (50 loc) · 1.07 KB
/
ArrayStack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// Created by light on 19-11-7.
//
#ifndef ALG_ARRAYSTACK_H
#define ALG_ARRAYSTACK_H
#include "../array/array.h"
#include "../interface.h"
#include "../stack/ArrayStack.h"
// 基于动态数组的Stack
template<typename T>
class ArrayStack : public CommonStack<T> {
private:
Array<T> *array;
public:
ArrayStack(int capacity) {
array = new Array<T>(capacity);
}
ArrayStack() {
array = new Array<T>();
}
~ArrayStack() {
delete[]array;
}
int getSize() {
return array->getSize();
}
bool isEmpty() {
return array->isEmpty();
}
int getCapacity() {
return array->getCapacity();
}
// 入栈策略:尾部添加 O(1)
void push(T e) {
array->addLast(e);
}
// 出栈策略:尾部出 O(1)
void pop() {
array->removeLast();
}
// O(1)
T peek() {
return array->getLast();
}
friend ostream &operator<<(ostream &out, ArrayStack &stack) {
out << *(stack.array);
return out;
}
};
#endif //ALG_ARRAYSTACK_H