-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.hpp
More file actions
282 lines (259 loc) · 5.42 KB
/
Copy pathStack.hpp
File metadata and controls
282 lines (259 loc) · 5.42 KB
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
#pragma once
#include "LinkedList.hpp"
template<class T>
class Stack{
private:
class Node{
private:
T m_data;
Node* m_pNext;
friend class Stack;
};
Node* m_pRoot;
Node* m_pLast;
// Accesors
size_t m_size;
// Modifiers
Node*generateNode(T t_data);
public:
// Constructors
Stack();
Stack(T t_data);
Stack(T t_array[], size_t t_size);
// Modifiers
void push_front(T t_data);
T pop_front();
void clear();
LinkedList<T> sort();
// Accesors
T lastData();
T at(size_t t_index);
bool empty();
void printList();
T top();
T peek();
size_t size();
};
/**
* @brief Creates a default Stack class.
*
*/
template<class T>
Stack<T>::Stack(){
m_pRoot = nullptr;
m_pLast = nullptr;
m_size = 0;
}
/**
* @brief Creates a Stack class with element given as parameter.
* @param t_data is the value passed through to the data in the stack.
*/
template<class T>
Stack<T>::Stack(T t_data){
m_pRoot = generateNode(t_data);
m_pRoot->m_pNext = nullptr;
m_pLast = m_pRoot;
m_size = 1;
}
/**
* @brief Creates a Stack class with all the elements from the array given.
* @param t_array passes an array to build a stack.
* @param t_arraySize is the size of the Stack List about to be built.
*/
template<class T>
Stack<T>::Stack(T t_array[], size_t t_sizeArray){
if(t_array == NULL){
for(size_t i = 0; i < t_sizeArray; i++){
push_front(0);
}
}
for(size_t i = 0; i < t_sizeArray; i++){
push_front(t_array[i]);
}
}
/**
* @brief Generates a Node with new data.
* @param t_data is the value passed through to the data in the stack.
*/
template<class T>
typename Stack<T>::Node*
Stack<T>::generateNode(T t_data){
Node* pNewNode = new Node();
pNewNode->m_data = t_data;
pNewNode->m_pNext = nullptr;
return pNewNode;
}
/**
* @brief Pushes data at the stack.
* @param t_data is the value passed through to the data in the stack.
*/
template<class T>
void Stack<T>::push_front(T t_data){
m_size++;
if(!m_pRoot){
m_pRoot = generateNode(t_data);
m_pLast = m_pRoot;
return;
}
Node* newNode = new Node();
newNode = generateNode(t_data);
newNode->m_pNext = m_pRoot;
m_pRoot = newNode;
}
/**
* @brief Pops out the first value from the stack and returns the value removed.
*
*/
template<class T>
T Stack<T>::pop_front(){
if(!m_pRoot){
throw std::length_error("Stack is empty");
}
if(!m_pRoot->m_pNext){
Node* pTmp = m_pRoot;
T data = pTmp->m_data;
delete[] m_pRoot;
m_pRoot = nullptr;
m_pLast = nullptr;
//delete[] pTmp;
m_size--;
return data;
}
Node* pTmp = m_pRoot;
T data = pTmp->m_data;
m_pRoot = m_pRoot->m_pNext;
delete[] pTmp;
m_size--;
return data;
/*
T returnValue = m_pRoot->m_data;
Node* deleteNode = m_pRoot;
if(m_pRoot->m_pNext){
m_pRoot = m_pRoot->m_pNext;
}
delete [] deleteNode;
deleteNode = nullptr;
return returnValue;*/
}
/**
* @brief Deletes all the queue.
*/
template<class T>
void Stack<T>::clear(){
if(!m_pRoot){
return;
}
Node* deleteNode = new Node();
deleteNode = nullptr;
while(m_pRoot){
m_pRoot->m_data = 0;
deleteNode = m_pRoot;
m_pRoot = m_pRoot->m_pNext;
delete [] deleteNode;
deleteNode = nullptr;
m_size--;
}
m_pLast->m_pNext = nullptr;
m_pLast = nullptr;
}
/**
* @brief Returns a LinkedList with the elements of the Stack sorted out. Uses Bubble Sort, time complexity: O(n^2).
*/
template<class T>
LinkedList<T> Stack<T>::sort(){
if(!m_pRoot){
return;
}
int tempArray[m_size];
Node* pTemp = new Node();
pTemp = m_pRoot;
for(size_t i = 0; i < m_size; i++){
tempArray[i] = pTemp->m_data;
pTemp = pTemp->m_pNext;
}
pTemp = nullptr;
int it = 0;
while(it < m_size-1){
for(size_t i = 0; i < m_size-1; i++){
if(tempArray[i] > tempArray[i+1]){
int temp = tempArray[i+1];
tempArray[i+1] = tempArray[i];
tempArray[i] = temp;
}
}
it++;
}
it = 0;
LinkedList<T> newLList(tempArray, m_size);
return newLList;
}
/**
* @brief Returns the last element of the stack
*
*/
template <class T>
T Stack<T>::lastData(){
if(!m_pRoot) {
return;
}
return m_pRoot->m_data;
}
/**
* @brief Returns the value at a specified location in the Stack
* @param t_index is the position of the data in the stack.
*/
template<class T>
T Stack<T>::at(size_t t_index){
if(t_index >= m_size) {
throw std::out_of_range("Invalid Index. Code: uwu");
}
Node* tempData = new Node();
tempData = m_pRoot;
for(size_t i = 0; i < t_index; i++){
tempData = tempData->m_pNext;
}
return tempData->m_data;
}
/**
*@brief Returns true if the queue is empty.
*/
template<class T>
bool Stack<T>::empty(){
return !m_pRoot;
}
/**
*@brief Prints content of the queue in order from first to last.
* Returns "Stack is empty" when the stack is empty.
*/
template <class T>
void Stack<T>::printList(){
if(!m_pRoot){
throw std::length_error("stack is empty");
return;
}
Node* temp = m_pRoot;
while(temp){
std::cout<<temp->m_data<<" ";
temp = temp->m_pNext;
}
std::cout<<std::endl;
}
template<class T>
T Stack<T>::top(){
if(!m_pRoot){
throw std::length_error("stack is empty");
}
if(!m_pRoot->m_pNext){
return m_pRoot->m_data;
}
return m_pLast->m_data;
}
// https://www.youtube.com/watch?v=xli_FI7CuzA FOR BUBBLE SORT
/**
*@brief Prints all the data in the nodes in the linked list
*
*/
template <class T>
size_t Stack<T>::size(){
return m_size;
}