-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.hpp
More file actions
412 lines (386 loc) · 8.28 KB
/
Copy pathLinkedList.hpp
File metadata and controls
412 lines (386 loc) · 8.28 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma once
#include <iostream>
// nested class
/**
*
*
*/
template <class T>
class LinkedList {
private:
// Constructors
class Node{
private:
T m_data;
Node* m_pNext;
friend class LinkedList;
};
Node* m_pRoot;
Node* m_pLast;
// Accesors
size_t m_size;
// Modifiers
Node* generateNode(T t_data);
public:
// Constructors
LinkedList();
LinkedList(T t_data);
LinkedList(T t_array[], size_t t_arraySize);
// Modifiers
void push_back(T t_data);
void push_front(T t_data);
T 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
T nodeData();
bool empty();
size_t rootData();
Node* returnRoot();
Node* returnLast();
void printList();
T at(size_t t_index);
size_t size();
};
/**
*
*
*/
template <class T>
LinkedList<T>::LinkedList(){
m_pRoot = nullptr;
m_pLast = nullptr;
m_size = 0;
}
/**
*
*
*/
template <class T>
LinkedList<T>::LinkedList(T t_data){
m_pRoot = generateNode(t_data);
m_pLast = 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>
LinkedList<T>::LinkedList(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++;
}
Node* tempLast = m_pRoot;
while(tempLast->m_pNext){
tempLast = tempLast->m_pNext;
}
m_pLast = tempLast;
m_pLast->m_pNext = nullptr;
}
/**
* @brief Generates new nodes
*
*/
template <class T>
typename LinkedList<T>::Node* LinkedList<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 LinkedList<T>::push_back(T t_data){
if(!m_pRoot){
//throw std::logic_error("UwUxecption");
m_pRoot = generateNode(t_data);
m_pLast = m_pRoot;
m_size++;
return;
}
m_pLast->m_pNext = generateNode(t_data);
m_pLast = m_pLast->m_pNext;
m_size++;
}
/**
* @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 LinkedList<T>::push_front(T t_data){
m_size++;
if(!m_pRoot){
m_pRoot = generateNode(t_data);
return;
}
Node* newNode = generateNode(t_data);
newNode->m_pNext = m_pRoot;
m_pRoot = newNode;
}
///////////////////////////////////
/**
* @brief Deletes the first node
*
*/
template<class T>
T LinkedList<T>::pop_front(){
// ELIMINAR UN PUNTERO
if(!m_pRoot){
throw std::length_error("List 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;
}
/////////
/**
* @brief Pops out the last node
*
*/
template<class T>
void LinkedList<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 LinkedList<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 LinkedList<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 LinkedList<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 LinkedList<T>::clear(){
if(!m_pRoot){
return;
}
while(m_pRoot->m_pNext){
Node* deleteNode = m_pRoot;
m_pRoot = m_pRoot->m_pNext;
delete [] deleteNode;
}
m_pRoot = nullptr;
}
/**
*@brief Deletes all nodes all clear the memory
*
*/
template<class T>
T LinkedList<T>::nodeData(){
return m_pRoot->m_data;
}
/**
* Returns true if the Linked List is empty
*/
template <class T>
bool LinkedList<T>::empty(){
return !m_pRoot;
}
/**
*@brief Prints all the data in the nodes in the linked list
*
*/
template <class T>
typename LinkedList<T>::Node*
LinkedList<T>::returnRoot(){
return m_pRoot;
}
/**
*@brief Prints all the data in the nodes in the linked list
*
*/
template <class T>
typename LinkedList<T>::Node*
LinkedList<T>::returnLast(){
return m_pLast;
}
/**
*@brief Prints all the data in the nodes in the linked list
*
*/
template <class T>
void LinkedList<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;
}
std::cout<<std::endl;
return;
}
std::cout<<"The list is empty.\n";
}
// ```cpp
// ```
/**
*@brief Prints all the data in the nodes in the linked list
*
*/
template <class T>
T LinkedList<T>::at(size_t t_index){
if(!m_pRoot){
return;
}
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 Prints all the data in the nodes in the linked list
*
*/
template <class T>
size_t LinkedList<T>::size(){
return m_size;
}