-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
82 lines (59 loc) · 994 Bytes
/
Copy pathStack.cpp
File metadata and controls
82 lines (59 loc) · 994 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <exception>
#include "Stack.h"
Stack::Stack()
{
_data = new int[_capacity];
}
Stack::~Stack()
{
delete[] _data;
}
bool Stack::IsEmpty()
{
return (_length == 0) ? true : false;
}
int Stack::GetLength()
{
return _length;
}
int Stack::GetCapacity()
{
return _capacity;
}
void Stack::ResizeArray()
{
int* newArray = new int[_capacity * _growthFactor];
for (int i = 0; i < _capacity; i++)
{
newArray[i] = _data[i];
}
_capacity *= _growthFactor;
delete[] _data;
_data = newArray;
}
void Stack::Push(int element)
{
if (_length >= _capacity)
{
ResizeArray();
}
_data[_length] = element;
_length++;
}
int Stack::Pop()
{
if (_length == 0)
{
throw std::exception("Stack is empty\n");
}
--_length;
return _data[_length];
}
int Stack::GetTop()
{
if (_length == 0)
{
throw std::exception("Stack is empty\n");
}
return _data[_length - 1];
}