-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
233 lines (195 loc) · 4.76 KB
/
LinkedList.cpp
File metadata and controls
233 lines (195 loc) · 4.76 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
#include "LinkedList.h"
#include <iostream>
#include <string>
template <class T>
void LinkedList<T>::Insert (T const data, int const insertPosition, const bool print)
{
if ((insertPosition > lengthOfList) || (insertPosition < 0))
{
std::cout << " ERROR: You entered a position larger than the size of your list + 1. The current length of the list is: " << lengthOfList << std::endl;
return;
}
else
{
DataNode<T>* newData = new DataNode<T>;
newData->data = data;
newData->next = nullptr;
if (insertPosition == 0) // change the Head node
{
if (head == NULL)
{
head = newData;
tail = newData;
lengthOfList++;
}
else
{
newData->next = head;
head = newData;
lengthOfList++;
}
}
else if (insertPosition == lengthOfList)
{
tail->next = newData;
tail = newData;
lengthOfList++;
}
else
{
DataNode<T>* previous = head;
DataNode<T>* current = head->next;
for (int i = 1; i < insertPosition; i++)
{
previous = previous->next;
current = current->next;
}
previous->next = newData;
newData->next = current;
lengthOfList++;
}
if (print)
std::cout << "Added '" << data << "' to the list at position " << insertPosition << std::endl;
}
}
template <class T>
void LinkedList<T>::Push(T const data, const bool print)
{
Insert(data, 0, print);
}
template <class T>
void LinkedList<T>::Append(T const data, const bool print)
{
Insert(data, lengthOfList, print);
}
template <class T>
int LinkedList<T>::Length(bool const print)
{
if (print)
std::cout << "There are " << lengthOfList << " elements in the list." << std::endl;
return lengthOfList;
}
template <class T>
void LinkedList<T>::Print()
{
if (lengthOfList < 1) // if list is empty
{
std::cout << "ERROR: There is no data in the list to print." << std::endl;
return;
}
else
{
DataNode<T>* tempHead = head;
while (tempHead->next != nullptr)
{
std::cout << tempHead->data << ", ";
tempHead = tempHead->next;
}
std::cout << tempHead->data << std::endl;
}
}
template <class T>
void LinkedList<T>::TailPrint()
{
if (tail != nullptr)
std::cout << "Last value in the list is: " << tail->data << std::endl;
else
std::cout << "ERROR: The list is empty." << std::endl;
}
template <class T>
void LinkedList<T>::Remove(int const elementPosition, const bool print) // 1 -> ... lengthOfList.
{
if (head == nullptr)
{
std::cout << "ERROR: The list has no data to delete" << lengthOfList << std::endl;
return;
}
else if ((elementPosition > lengthOfList - 1) || (elementPosition < 0))
{
std::cout << "ERROR: Element passed is outside the range of the list. Reference starts at '0' : The number of elements in the list: " << lengthOfList << std::endl;
return;
}
else if (elementPosition == 0)
{
if (lengthOfList > 1)
{
DataNode<T>* toDelete = head;
head = head->next;
delete toDelete;
if (print)
std::cout << "Removed the 1st element from the list" << std::endl;
lengthOfList--;
}
else
{
delete head;
if (print)
std::cout << "Removed the 1st element from the list. The list is now empty" << std::endl;
head = tail = nullptr;
lengthOfList--;
}
}
else
{
int currentElement = 1;
DataNode<T>* previous = head;
while (currentElement != elementPosition)
{
previous = previous->next;
currentElement++;
}
DataNode<T>* toDelete = previous->next;
previous->next = toDelete->next;
if (previous->next == nullptr) // if we are deleting the last Datanode - set tail to the previous node)
{
tail = previous;
}
delete toDelete;
if (print)
std::cout << "Removed the element in position " << currentElement << " from the list." << std::endl;
lengthOfList--;
}
}
template <class T>
void LinkedList<T>::Pop(const bool print)
{
Remove(lengthOfList - 1);
if (print)
std::cout << "The last element in the list was deleted" << std::endl;
}
template <class T>
void LinkedList<T>::Pop(T &data, const bool print)
{
if (lengthOfList < 1)
{
std::cout << "ERROR: The list has no data to delete - Pop Failed" << std::endl;
return;
}
else
{
data = tail->data;
Remove(lengthOfList - 1);
if (print)
std::cout << "i grabbed the data '" << data << "' from the end of the list, stored it in the variable provided and then deleted it from the list.\n" << std::endl;
}
}
template <class T>
LinkedList<T>::~LinkedList()
{
std::cout << "Destroying all elements in the list..." << std::endl;
if (head == NULL)
{
std::cout << "There was no nodes to remove... All data is removed" << std::endl;
}
else
{
std::cout << "The length of the list is: " << lengthOfList << std::endl;
int counter = 0;
while (lengthOfList != 0)
{
Remove(0);
counter++;
}
std::cout << counter << " nodes were deleted from the heap. \n\nAll nodes removed." << std::endl;
}
}