-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
113 lines (92 loc) · 1.57 KB
/
Stack.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Honor Pledge:
//
// I pledge that I have neither given nor
// received any help on this assignment.
//
// blakbenn
//
// Stack
//
template <typename T>
Stack <T>::Stack (void):
//build an array object to store data
arr(Array<T>())
{
}
//
// Stack
//
template <typename T>
Stack <T>::Stack (const Stack & stack):
//set the Arrays equal
arr(stack.arr)
{
}
//
// ~Stack
//
template <typename T>
Stack <T>::~Stack (void)
{
//handled by ~BaseArray()
}
//
// push
//
template <typename T>
void Stack <T>::push (T element)
{
//check for the empty stack
if(this->is_empty())
{
this->arr.resize(1);
//if it's empty, set the first element in the 0 spot
this->arr.set(this->arr.size()-1, element);
}
else
{
//otherwise, set the element at the new top of the stack (arr.size()+1)
this->arr.resize(this->size() + 1);
this->arr.set(this->arr.size()-1, element);
}
}
//
// pop
//
template <typename T>
void Stack <T>::pop (void)
{
//check for the empty stack
if(this->is_empty())
{
throw empty_exception();
}
else
{
//if the stack isn't empty, resize
this->arr.resize(arr.size() - 1);
}
}
//
// operator =
//
template <typename T>
const Stack <T> & Stack <T>::operator = (const Stack & rhs)
{
//check for self assignment
if (this == &rhs)
{
return *this;
}
//set the Arrays equal
this->arr = rhs.arr;
return *this;
}
//
// clear
//
template <typename T>
void Stack <T>::clear (void)
{
this->arr.resize(0);
}