-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.hpp
More file actions
346 lines (322 loc) · 7.37 KB
/
Copy pathCircularLinkedList.hpp
File metadata and controls
346 lines (322 loc) · 7.37 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#pragma once
#include <iostream>
// nested class
/**
*
*
*/
template <class T>
class CircularLinkedList {
private:
// Constructors
class Node{
private:
T m_data;
Node* m_pNext;
friend class CircularLinkedList;
};
Node* m_pRoot;
Node* m_pLast;
// Accesors
size_t m_size;
// Modifiers
Node* generateNode(T t_data);
public:
// Constructors
CircularLinkedList();
CircularLinkedList(T t_data);
CircularLinkedList(T t_array[], size_t t_arraySize);
// Modifiers
void push_back(T t_data);
void push_front(T t_data);
void pop_front();
void pop_back();
void insert(T t_data, size_t t_index);
void erase(size_t t_index);
void eraseAll(T t_data);
void clear(); // Libera toda la memoria usada por la lista ligada.
// Accesors
size_t nodeData();
bool empty();
size_t rootData();
Node* returnRoot();
void printList();
};
/**
* @ brief Default Constructor, size = 0.
*
*/
template <class T>
CircularLinkedList<T>::CircularLinkedList(){
m_pRoot = nullptr;
m_pLast = nullptr;
m_size = 0;
}
/**
* @brief Constructor with one element.
* @param t_data is the data in the node.
*/
template <class T>
CircularLinkedList<T>::CircularLinkedList(T t_data){
m_pRoot = generateNode(t_data);
m_pLast = m_pRoot;
m_pLast->m_pNext = m_pRoot;
m_size = 1;
}
/**
*@brief Creates a Linked List class with the array given as parameter
*@param The array used to create the Linked List
*/
template <class T>
CircularLinkedList<T>::CircularLinkedList(T t_array[], size_t t_arraySize){
//std::cout<<"this\n";
//m_pRoot = generateNode(t_array[0]);
m_pRoot = nullptr;
m_size = 0;
m_pLast = m_pRoot;
//std::cout<<"arry: "<<m_pLast->m_data<<"\n";
for(size_t i = 0; i < t_arraySize; i++){
push_back(t_array[i]);
//std::cout<<"size: "<<m_size<<"\n";
//std::cout<<"arry: "<<m_pLast->m_data<<"\n";
//m_size++;
}
m_pLast->m_pNext = m_pRoot;
}
/**
* @brief Generates new nodes
*
*/
template <class T>
typename CircularLinkedList<T>::Node* CircularLinkedList<T>::generateNode(T t_data){
Node* pNewNode = new Node();
pNewNode->m_data = t_data;
pNewNode->m_pNext = nullptr;
return pNewNode;
}
/**
* @brief Adds a node to the end of the list
* @param The node has the data given
*/
template <class T>
void CircularLinkedList<T>::push_back(T t_data){
m_size++;
if(!m_pRoot){
//throw std::logic_error("UwUxecption");
m_pRoot = generateNode(t_data);
m_pLast = m_pRoot;
m_pLast->m_pNext = m_pRoot;
return;
}
m_pLast->m_pNext = generateNode(t_data);
m_pLast = m_pLast->m_pNext;
m_pLast->m_pNext = m_pRoot;
}
/**
* @brief Add a node to the beggining of the list
* @param t_data is the new data added to the node
*/
template<class T>
void CircularLinkedList<T>::push_front(T t_data){
m_size++;
if(!m_pRoot){
m_pRoot = generateNode(t_data);
m_pRoot->m_pNext = m_pRoot;
m_pLast = m_pRoot;
m_pLast->m_pNext = m_pRoot;
return;
}
Node* newNode = generateNode(t_data);
newNode->m_pNext = m_pRoot;
m_pRoot = newNode;
m_pLast->m_pNext = m_pRoot;
}
///////////////////////////////////
/**
* @brief Deletes the first node
*
*/
template<class T>
void CircularLinkedList<T>::pop_front(){
// ELIMINAR UN PUNTERO
if(!m_pRoot){
return;
}
Node* pTemp = new Node();
pTemp = m_pRoot;
m_pRoot = m_pRoot->m_pNext;
delete[] pTemp;
pTemp = nullptr;
m_size--;
}
/////////
/**
* @brief Pops out the last node
*
*/
template<class T>
void CircularLinkedList<T>::pop_back(){
if(!m_pRoot){
return;
}
if(!m_pRoot->m_pNext){
//std::cout<<"this\n";
delete[] m_pRoot;
m_pRoot = nullptr;
m_size = 0;
return;
}
std::cout<<"mpRoot data: "<<m_pRoot->m_data<<std::endl;
Node* pTemp = new Node();
pTemp = m_pRoot;
while (pTemp->m_pNext->m_pNext){
pTemp = pTemp->m_pNext;
std::cout<<"pTemp data: "<<pTemp->m_data<<std::endl;
}
//std::cout<<"this2\n";
delete []pTemp->m_pNext;
pTemp->m_pNext = nullptr; //este puntero causa muchos problemas !!!!!!!!!!!
//std::cout<<"this3\n";
m_pLast = pTemp;
//std::cout<<"this4\n";
m_size--;
}
/**
* @brief Inserts a Node with the data given at the index passed through
* @parama The data to give to the node, and the index position
*/
template<class T>
void CircularLinkedList<T>::insert(T t_data, size_t t_index){ // Agrega un nodo con el t_data en el índice t_index.
if(t_index > m_size){
throw std::out_of_range("Invalid Index. Code: uwu");
return;
}
//Node* previousPointer = new Node();
Node* newNode = generateNode(t_data);
if(t_index == 0){
newNode->m_pNext = m_pRoot;
m_pRoot = newNode;
} else {
Node* prevNode = m_pRoot;
if(!m_pRoot->m_pNext){
newNode->m_pNext = m_pRoot->m_pNext;
m_pRoot->m_pNext = newNode;
}
// Find the node at t_index - 1
for(size_t i = 0; i < t_index - 1; ++i){
prevNode = prevNode->m_pNext;
}
// here ->next
newNode->m_pNext = prevNode->m_pNext;
prevNode->m_pNext = newNode;
}
m_size++;
}
/**
* @brief Deletes a node at the index given
* @param Index for the node to delete
*/
template<class T>
void CircularLinkedList<T>::erase(size_t t_index){
if(t_index >= m_size||!m_pRoot){
return;
}
if(t_index==0){
// Erase the first node
Node* temp = m_pRoot;
m_pRoot = m_pRoot->m_pNext;
delete []temp;
}else{
// Find the node at t_index -1
Node* prevNode = m_pRoot;
for(size_t i=0; i<t_index-1; i++){
prevNode = prevNode->m_pNext;
}
Node* nodeToDelete = prevNode->m_pNext;
prevNode->m_pNext = nodeToDelete->m_pNext;
delete []nodeToDelete;
}
--m_size;
}
/**
* @brief Deletes all the nodes with the same value as t_data
* @param All the nodes with the same data will be deleted
*/
template<class T>
void CircularLinkedList<T>::eraseAll(T t_data){
if(!m_pRoot){
return;
}
while(m_pRoot && m_pRoot->m_data == t_data){
Node* deleteNode = m_pRoot;
m_pRoot = m_pRoot->m_pNext;
delete [] deleteNode;
m_size--;
}
Node* temp = m_pRoot;
//Node* prevNode = new Node();
while(temp && temp->m_pNext){
if(temp->m_pNext->m_data == t_data){
//std::cout<<"this\n";
Node* deleteNode = temp->m_pNext;
if(temp->m_pNext->m_pNext){
temp->m_pNext = temp->m_pNext->m_pNext;
}
else{
temp->m_pNext = nullptr; // ESTE PUNTERO TAMBIÉN DA PROBLEMAS!!!!!
}
delete [] deleteNode;
m_size--;
} else {
temp = temp->m_pNext;
}
//m_pLast = temp;
}
m_pLast = temp;
}
/**
*@brief Deletes all nodes all clear the memory
*
*/
template<class T>
void CircularLinkedList<T>::clear(){
if(!m_pRoot){
return;
}
for(size_t i = 0; i < m_size; i++ ){
pop_front();
}
m_pRoot = nullptr;
}
/**
* Returns true if the Linked List is empty
*/
template <class T>
bool CircularLinkedList<T>::empty(){
return !m_pRoot;
}
/**
*@brief Prints all the data in the nodes in the linked list
*
*/
template <class T>
void CircularLinkedList<T>::printList(){
Node* temp = new Node();
temp = m_pRoot;
//temp->m_pNext = m_pRoot->m_pNext;
if(m_pRoot) {
std::cout<<"The list contains: ";
while(temp) {
std::cout<<temp->m_data<<" ";
temp = temp->m_pNext;
if(temp == m_pRoot){
break;
}
}
std::cout<<std::endl;
return;
}
std::cout<<"The list is empty.\n";
}
// ```cpp
// ```