-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.h
324 lines (290 loc) · 6.5 KB
/
linkedList.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
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
// CLASS TEMPLATE CREDIT GOES TO https://github.com/SloCompTech/QList
// I edited this template class for the purposes of the project
#ifndef HW3_LINKEDLIST
#define HW3_LINKEDLIST
#ifndef NULL
#define NULL 0
#endif
template<class T>
class LinkedList
{
public:
typedef struct node
{
T item;
node *next,*prev;
}node;
private:
int len; // Size of list
node *start,*end; // Pointers to start and end
public:
LinkedList(); // Class constructor
~LinkedList(); // Class destructor
node& push_back(const T i); // Push item at the back of list
node& push_front(const T i);// Push item at the front of the list
void pop_back(); // Pops item from back
void pop_front(); // Pops item from front
LinkedList<T>::node * front(); // get item from front
LinkedList<T>::node * back(); // get item from back
int size(); // Returns size of list
void clear(); // Clears list
void remove(LinkedList<T>::node *);
void clear(unsigned int index); // Clears list
T get(unsigned int index); // Get item at given index
T& at(unsigned int index); // Get item at given index
//CONTAINS
bool contains(T);
// Array operator
T& operator[](unsigned int index);
const T& operator[](unsigned int index) const; // Not realy needed
// Non - critical functions
int length();
int indexOf(T val);
};
// Constructor
template<class T>
LinkedList<T>::LinkedList()
{
len = 0;
start = NULL;
end = NULL;
}
// Destructor
template<class T>
LinkedList<T>::~LinkedList()
{
clear();
}
// Push at front
template<class T>
typename LinkedList<T>::node & LinkedList<T>::push_front(const T i)
{
node *tmp = new node;
tmp->item = i;
tmp->next = NULL;
tmp->prev = NULL;
if(start==NULL) // If list is empty
{
start = tmp;
end = tmp;
}
else // Insert at start
{
tmp->next = start;
start->prev = tmp;
start = tmp;
}
len++; // Increase size counter
return *tmp;
}
// Push at back
template<class T>
typename LinkedList<T>::node & LinkedList<T>::push_back(const T i)
{
node *tmp = new node;
tmp->item = i;
tmp->next = NULL;
tmp->prev = NULL;
if(end==NULL) // If list is empty
{
start = tmp;
end = tmp;
}
else // Insert at the end
{
tmp->prev = end;
end->next = tmp;
end = tmp;
}
len++; // Increase size counter
return *tmp;
}
// Pop from front
template<class T>
void LinkedList<T>::pop_front()
{
if(start!=NULL)
{
node *tmp = start;
start = start->next;
if(start!=NULL) // Re-link next item to NULL
start->prev = NULL;
else // List became empty so we need to clear end
end = NULL;
delete tmp;
len--; // Decrease counter
}
}
// Pop from back
template<class T>
void LinkedList<T>::pop_back()
{
if(end!=NULL)
{
node *tmp = end;
end = end->prev;
if(end!=NULL) //Re-link previous item to NULL
end->next = NULL;
else // List became empty so we need to clear start
start = NULL;
delete tmp;
len--; // Decrease counter
}
}
template<class T>
void LinkedList<T>::remove(LinkedList<T>::node * listNode) {
if (listNode == start) {
pop_front();
} else if (listNode == end) {
pop_back();
} else {
if(listNode->prev!=NULL)
listNode->prev->next = listNode->next;
else
start = listNode->next;
if(listNode->next!=NULL)
listNode->next->prev = listNode->prev;
else
end = listNode->prev;
len--; // Decrease counter
delete listNode; // Delete item
}
}
template<class T>
bool LinkedList<T>::contains(T checkItem) {
node *tmp = start;
while(tmp!=NULL)
{
if (tmp->item == checkItem) {
return true;
}
tmp = tmp->next;
}
return false;
}
// Get item from front
template<class T>
typename LinkedList<T>::node * LinkedList<T>::front()
{
return start;
}
//Get item from back
template<class T>
typename LinkedList<T>::node * LinkedList<T>::back()
{
return end;
}
// Get size
template<class T>
int LinkedList<T>::size()
{
return this->len;
}
// Clear list
template<class T>
void LinkedList<T>::clear()
{
node *tmp = start;
while(start!=NULL)
{
tmp = start;
start = start->next;
delete tmp; // Delete item
len--; // Decrease counter
}
end = NULL;
}
template<class T>
void LinkedList<T>::clear(unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
{
if(i==index)
{
if(tmp->prev!=NULL)
tmp->prev->next = tmp->next;
else
start = tmp->next;
if(tmp->next!=NULL)
tmp->next->prev = tmp->prev;
else
end = tmp->prev;
len--; // Decrease counter
delete tmp; // Delete item
break;
}
else
tmp=tmp->next;
}
}
// Get at index
template<class T>
T LinkedList<T>::get(unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
{
if(i==index)
return tmp->item;
else
tmp=tmp->next;
}
//TODO: Catch error when index is out of range
}
template<class T>
T& LinkedList<T>::at(unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
{
if(i==index)
return tmp->item;
else
tmp=tmp->next;
}
//TODO: Catch error when index is out of range
}
// Get length
template<class T>
int LinkedList<T>::length()
{
return this->len;
}
// Get index of value
template<class T>
int LinkedList<T>::indexOf(T val)
{
for(int i=0;i<this->size();i++)
if(this->at(i) == val)
return i;
return -1;
}
// Array operators
template<class T>
T& LinkedList<T>::operator[](unsigned int index)
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
{
if(i==index)
return tmp->item;
else
tmp=tmp->next;
}
//TODO: Catch error when index is out of range
}
template<class T>
const T& LinkedList<T>::operator[](unsigned int index) const
{
node *tmp = start;
for(int i=0;i<=index&&tmp!=NULL;i++)
{
if(i==index)
return tmp->item;
else
tmp=tmp->next;
}
//TODO: Catch error when index is out of range
}
#endif