-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack.h
284 lines (251 loc) · 6.08 KB
/
stack.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#ifndef STACK_EXAMPLE_STACK_H
#define STACK_EXAMPLE_STACK_H
#include <iostream>
#include <exception>
#include <stdexcept>
#include "StackItem.h"
#define ZERO 0
#define EMPTY_STACK "Empty stack"
#define STACK_CONTENT "Stack content:"
using std::cout;
using std::endl;
/*
* stack
* Implements a stack data structure by singley (one-way) linked list.
* Makes use of the StackItem class that stores int / float / string
* and the pointer to the next element.
* By itself stores a pointer to the first element in the linked list,
* and how many elements there are in total.
* Allows using pop and push functions to manipulate.
* print, empty, size, top functions to get additional info.
*
*/
template <class T>
class stack
{
public:
// Empty stack construct
stack() : _first(NULL), _size(ZERO) {}
/*
* Copy constructor
* makes use of reverseCopy function to create the copy while
* maintaining the original element order.
*
* @param const stack &otherStack stack to be copied
*/
stack(const stack &otherStack);
/*
* operator=
* makes use of reverseCopy function to
* maintain the original element order.
*
* @param const stack &rightHandSide stack to be copied
*/
stack& operator=(const stack &rightHandSide);
/*
* empty
* const function
* Checks whether stack is empty or not
*
* @return bool true if empty, false otherwise
*/
bool empty() const
{
return _size == ZERO;
}
/*
* size
* const function
* Retrieves number of elements in the stack
*
* @return size_t _size
*/
size_t size() const
{
return _size;
}
/*
* pop
* Extracts(returns value and deletes) the top element from stack,
* updates the top element and the number of elements,
* If stack is empty throws Empty stack
*
* ***handles memory deletion***
*
* @return T value of top element in stack before manipulation
*/
T pop();
/*
* top
* const function
* Retrieves the value of the top element in the stack.
* If empty throws "Empty stack"
*
* @return T value of the top element in the stack.
*/
T top() const;
/*
* push
* Inserts a new element to the top stack,
* updates (_first) accordingly and the number of elements (_size)
* Handles POINTER_ALREADY_SET exception from StackItem<T>::setNext
*
* @param T item the value to be stored
* @return void
*/
void push(T item);
// Prints all stack elements by order, one element per row.
/*
* print
* displays all values, by order, stored in the stack.
* const function
*
* @return void
*/
void print() const;
// Class Destructor
~stack()
{
try
{
destroy();
}
// Handles potential runtime error (Empty stack) from pop from destroy
catch (const std::exception &error)
{
std::cerr << error.what() << endl;
}
}
private:
StackItem<T> *_first; // stores pointer to the _first (top) StackItem in stack
size_t _size; // stores how many elements in stack
/*
* destroy
* pops all elements in stack
* internal use of destructor
*
* @return void
*/
void destroy();
/*
* reverseCopy
* Copies all element values from origin to destination, in reverse order
* used by copy and operator= only, to maintain original element value order
* Assumes destination is empty.
*
* @param const stack &origin the stack to be copied from
* @param const stack &destination the stack to be copied to
*
* @return void
*/
void reverseCopy(const stack &origin, stack &destination);
};
template <typename T>
stack<T>::stack(const stack<T> &otherStack): _first(NULL), _size(otherStack._size)
{
if(!otherStack.empty())
{
stack temp;
reverseCopy(otherStack, temp);
reverseCopy(temp, (stack<T> &) this);
}
}
template <typename T>
stack<T>& stack<T>::operator=(const stack<T> &rightHandSide)
{
if (this == &rightHandSide) return *this; //check for self assignment
if(!rightHandSide.empty())
{
// if the stack isn't empty, empties and only then copies new elements.
try
{
destroy();
}
// Handles potential runtime error (Empty stack) from pop from destroy
catch (const std::exception &error)
{
std::cerr << error.what() << endl;
}
_size = rightHandSide.size();
stack temp;
reverseCopy(rightHandSide, temp);
reverseCopy(temp, (stack<T> &) this);
}
return *this;
}
template <typename T>
void stack<T>::destroy()
{
while (!empty())
pop();
}
template <typename T>
T stack<T>::pop()
{
if (!empty())
{
T value = _first->getValue();
StackItem<T> *temp = _first;
_first = _first->getNext();
delete temp;
_size--;
return value;
}
else
{
throw std::range_error(EMPTY_STACK);
}
}
template <typename T>
T stack<T>::top() const
{
if (!empty())
{
return _first->getValue();
}
else
{
throw std::range_error(EMPTY_STACK);
}
}
template <typename T>
void stack<T>::push(T item)
{
// Allocate a new StackItem object
StackItem<T> *ptr = new StackItem<T>(item);
try
{
ptr->setNext(_first);
_first = ptr;
_size++;
}
catch (const std::exception &error)
{
std::cerr << error.what() << endl;
}
}
template <typename T>
void stack<T>::print() const
{
cout << (STACK_CONTENT) << endl;
if (!empty())
{
StackItem<T> *current = _first;
for (size_t i = ZERO; i < _size; i++)
{
cout << current->getValue() << endl;
current = current->getNext();
}
}
}
template <typename T>
void stack<T>::reverseCopy(const stack &origin, stack &destination)
{
StackItem<T> *current = origin._first;
for (size_t i = ZERO; i < origin._size; i++)
{
destination.push(current->getValue());
current = current->getNext();
}
}
#endif //STACK_EXAMPLE_STACK_H