-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSLinkedList.h
250 lines (225 loc) · 5.2 KB
/
DSLinkedList.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
//
// Created by Sammy Timmins on 9/18/20.
//
#ifndef INC_20F_AUTO_IDX_LINKEDLIST_H
#define INC_20F_AUTO_IDX_LINKEDLIST_H
#include <iostream>
template <typename T>
struct Node
{
T data;
Node *next;
Node *previous;
Node()
{
next = nullptr;
previous = nullptr;
}
Node(T data)
{
this->data = data;
next = nullptr;
previous = nullptr;
}
};
template <typename T>
class DSLinkedList
{
public:
DSLinkedList();
~DSLinkedList();
DSLinkedList(const DSLinkedList ©);
DSLinkedList& operator=(const DSLinkedList ©);
T& operator[](int index);
friend std::ostream& operator<<(std::ostream& os, DSLinkedList& list);
void add(const T &toAdd);
void remove(const T &toRemove);
T & at(int index) const;
int search(const T &toFind);
void print();
int getSize() const;
private:
Node<T> *head = nullptr;
Node<T> *tail = nullptr;
int size;
};
template <typename T>
DSLinkedList<T>::DSLinkedList()
{
head = nullptr;
tail = nullptr;
size = 0;
}
template <typename T>
DSLinkedList<T>::~DSLinkedList()
{
if(head != nullptr)
{
auto curr = this->head;
auto currNext = curr->next;
for(int i = 0; i < size; i++) //looping through the list and deallocating every node
{
delete curr;
curr = currNext;
if (curr != nullptr) {
currNext = curr->next;
}
}
}
}
template <typename T>
DSLinkedList<T>::DSLinkedList(const DSLinkedList ©)
{
this->head == nullptr;
this->tail == nullptr;
this->size = copy.size;
auto curr = copy.head;
while(curr != nullptr) //looping through list and copying the nodes from the list to be copied
{
auto currNext = curr->next;
add(curr->data);
curr = currNext;
}
}
template <typename T>
DSLinkedList<T> & DSLinkedList<T>::operator=(const DSLinkedList<T> ©)
{
if(*this != copy)
{
if(this->head != nullptr) //if the list is not empty it will deallocate what is currently held in the list
{
delete this;
}
this->head = nullptr;
this->tail = nullptr;
auto curr = copy.head;
while(curr != nullptr) //copies over the nodes from the list to be copied
{
add(curr->data);
curr = curr->next;
}
}
return *this;
}
template <typename T>
T& DSLinkedList<T>::operator[](const int index) //accesses a node at a specified index
{
if(index < size && index >= 0)
{
auto curr = head;
for(int i = 0; i < index; i++)
{
curr = curr->next;
}
return curr->data;
} else //range checking
{
std::cout << "Out of range" << std::endl;
exit(1);
}
}
template <typename T>
std::ostream& operator<<(std::ostream& os, DSLinkedList<T>& list)
{
auto curr = list.head;
for(int i = 0; i < list.size; i++)
{
os << curr->data << std::endl;
curr = curr->next;
}
return os;
}
template <typename T>
void DSLinkedList<T>::add(const T &toAdd) //adds a node to the end of the list
{
if(head == nullptr)
{
head = new Node<T>(toAdd);
tail = head;
++size;
} else
{
tail->next = new Node<T>(toAdd);
tail->next->previous = tail;
tail = tail->next;
++size;
}
}
template <typename T>
void DSLinkedList<T>::remove(const T &toRemove) //removes a node with the specified value
{
if(head->data == toRemove)
{
auto curr = head;
curr->next->previous == nullptr;
head = curr->next;
delete curr;
} else if(tail->data == toRemove)
{
auto curr = tail;
curr->previous->next = nullptr;
tail = curr->previous;
delete curr;
} else
{
auto curr = head;
while(curr != nullptr)
{
if(curr->data == toRemove)
{
curr->previous->next = curr->next;
curr->next->previous = curr->previous;
delete curr;
}
curr = curr->next;
}
}
--size;
}
template <typename T>
T & DSLinkedList<T>::at(int index) const { //accesses a node and returns the value at a specified index
if(index < size && index >= 0)
{
auto curr = head;
for(int i = 0; i < index; i++)
{
curr = curr->next;
}
return curr->data;
} else
{
std::cout << "Out of range" << std::endl;
exit(1);
}
}
template <typename T>
int DSLinkedList<T>::search(const T &toFind) //returns the index for a specified node
{
auto curr = head;
for(int i = 0; i < size; i++)
{
if(curr->data == toFind)
{
return i;
} else
{
curr = curr->next;
}
}
return -1;
}
template <typename T>
void DSLinkedList<T>::print()
{
auto curr = head;
for(int i = 0; i < size; i++)
{
std::cout << curr->data << std::endl;
curr = curr->next;
}
}
template <typename T>
int DSLinkedList<T>::getSize() const { //returns the size of the list
return size;
}
#endif //INC_20F_AUTO_IDX_LINKEDLIST_H