-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_array.cpp
More file actions
60 lines (48 loc) · 990 Bytes
/
Copy pathstack_array.cpp
File metadata and controls
60 lines (48 loc) · 990 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
51
52
53
54
55
56
57
58
59
60
/*
* 2019.6.28
* data structure implementation - stack_array
*/
#include <iostream>
using namespace std;
class StackArr {
public:
int s_size, capacity;
int *stack;
StackArr() {
s_size = 0;
capacity = 8;
stack = new int[capacity];
}
~StackArr() {
delete[] stack;
stack = NULL;
}
void push(int value) {
if (s_size == capacity) {
capacity *= 2;
int *temp = new int[capacity];
for (int i = 0; i < capacity / 2; i++) {
temp[i] = stack[i];
}
delete[] stack;
stack = temp;
}
stack[s_size++] = value;
}
int top() {
if (s_size == 0)
return -1;
return stack[s_size - 1];
}
void pop() {
if (s_size == 0)
return;
s_size--;
}
bool empty() {
return s_size == 0;
}
int size() {
return s_size;
}
};