-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArrayStack.cpp
70 lines (55 loc) · 1.28 KB
/
ArrayStack.cpp
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
66
67
68
69
70
#include <iostream>
template <class T>
class ArrayStack {
public:
// Creates empty stack with capacity of size 8 and size 0
ArrayStack(): _capacity{8}, _size{0}, array{new T[8]} { }
// Pushes an element onto the stack
void push(T elt) {
if (_size >= _capacity) { resize(); }
array[_size++] = elt;
}
// Removes an element from the stack
void pop() {
if (_size == 0) { throw "Attempt to pop from empty stack"; }
--_size;
}
// Returns the top most element in the stack (the newest element inserted)
T top() {
return array[_size - 1];
}
// Returns the size of the stack
unsigned int size() {
return _size;
}
// Returns whether or not the stack is empty
bool empty() {
return _size == 0;
}
private:
unsigned int _capacity;
unsigned int _size;
T* array;
// Resizes internal array by copying into new array and deleting old one
void resize() {
_capacity *= 2;
T *newArr = new T[_capacity];
for (int i = 0; i < _size; ++i) {
newArr[i] = array[i];
}
delete[] array;
array = newArr;
}
};
int main() {
// Create a stack of ints
ArrayStack<int> stack;
stack.push(3);
stack.push(2);
stack.push(1);
std::cout << "Top of stack: " << stack.top() << std::endl;
while (!stack.empty()) {
std::cout << stack.top() << std::endl;
stack.pop();
}
}