-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
52 lines (46 loc) · 1021 Bytes
/
Stack.cpp
File metadata and controls
52 lines (46 loc) · 1021 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
#include <iostream>
#include "Stack.h"
#include "StackException.h"
using namespace std;
//Only modify the pop function in this file.
// Sets size to 0 and dynamically allocates an array of size 25 for elements.
template <typename T>
Stack<T>::Stack() {
size = 0;
elements = new T[25];
}
// Returns true if size is 0, false if not.
template <typename T>
bool Stack<T>::isEmpty() const {
if (size == 0) {
return true;
} else {
return false;
}
}
// Pushes value onto stack if the size is less than 25.
template <typename T>
void Stack<T>::push(T value) {
if (size < 25) {
elements[size] = value;
size++;
} else {
return;
}
}
// Pops top element off stack.
// MODIFY THIS FUNCTION TO THROW A StackException OBJECT IF STACK IS EMPTY
template <typename T>
T Stack<T>::pop() {
if (size != 0) {
return elements[--size];
} else {
StackException err;
throw err;
}
}
// Returns the size of the stack.
template <typename T>
int Stack<T>::getSize() const {
return size;
}