-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_array.cpp
More file actions
50 lines (42 loc) · 871 Bytes
/
stack_array.cpp
File metadata and controls
50 lines (42 loc) · 871 Bytes
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
template <typename T>
class Stack {
T *data;
int size;
int capacity;
public:
Stack() {
capacity = 5;
data = new T[capacity];
size = 0;
}
void push(T element) {
if (size == capacity) {
T *newData = new T[capacity*2];
for (int i = 0; i < capacity; i++) {
newData[i] = data[i];
}
capacity *= 2;
data = newData;
}
data[size] = element;
size++;
}
bool isEmpty() {
return size == 0;
}
T pop() {
if (isEmpty()) {
return 0;
}
T element = data[size-1];
size--;
return element;
}
T top() {
T element = data[size-1];
return element;
}
int getSize() {
return size;
}
};