-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.hpp
508 lines (391 loc) · 8.92 KB
/
List.hpp
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <iostream>
using namespace std;
//_______________________________NESTED CONST_ITERATOR CLASS________________________
// Default zero-parameter constructor set pointer to NULL
template <typename T>
List<T>::const_iterator:: const_iterator():current{nullptr}
{
}
// Returns reference to the corresponding element in list
template <typename T>
const T& List<T>::const_iterator:: operator*() const
{
return retrieve();
}
// Prefix and Postfix increment operators
template <typename T>
typename List<T>::const_iterator& List<T>::const_iterator:: operator++()
{
current=current->next;
return *this;
}
template <typename T>
typename List<T>::const_iterator List<T>:: const_iterator::operator++(int)
{
List<T>::const_iterator old=*this;
++(*this);
return old;
}
// Prefix and Postfix decrement operators
template <typename T>
typename List<T>::const_iterator& List<T>::const_iterator:: operator--()
{
current=current->prev;
return *this;
}
template <typename T>
typename List<T>::const_iterator List<T>::const_iterator:: operator--(int)
{
List<T>::const_iterator old=*this;
--(*this);
return old;
}
// Two iterators are equal if referring to same element
template <typename T>
bool List<T>::const_iterator:: operator==(const List<T>::const_iterator & rhs)const
{
return current==rhs.current;
}
// Two iterators are not equal if not referring to same element
template <typename T>
bool List<T>::const_iterator:: operator!=(const List<T>::const_iterator & rhs)const
{
return !(*this==rhs);
}
// Return a reference to corresponding element in the list
template <typename T>
T & List<T>::const_iterator:: retrieve()const
{
return current->data;
}
template <typename T>
List<T>::const_iterator:: const_iterator(Node *p):current{p}
{
}
//___________________________________NESTED ITERATOR CLASS____________________________
// Default zero parameter constructor
template <typename T>
List<T>::iterator:: iterator()
{
}
// Returns a reference to corresponding element in list
template <typename T>
T & List<T>::iterator:: operator*()
{
return List<T>::const_iterator::retrieve();
}
template <typename T>
const T & List<T>::iterator:: operator*() const
{
return List<T>::const_iterator::operator*();
}
// Prefix and postfix increment
template <typename T>
typename List<T>::iterator& List<T>::iterator:: operator++()
{
this->current=this->current->next;
return *this;
}
template <typename T>
typename List<T>::iterator List<T>::iterator:: operator++(int)
{
List<T>::iterator old=*this;
++(*this);
return old;
}
// Prefix and postfix decrement
template <typename T>
typename List<T>::iterator& List<T>::iterator:: operator--()
{
this->current=this->current->prev;
return *this;
}
template <typename T>
typename List<T>::iterator List<T>::iterator:: operator--(int)
{
List<T>::iterator old=*this;
--(*this);
return old;
}
template <typename T>
List<T>::iterator::iterator(Node *p):List<T>::const_iterator{p}
{
}
//_________________________________LIST CLASS_________________________________________
// Default zero parameter constructor calls init
template <typename T>
List<T>::List()
{
init();
}
// Copy constructor creates new list using elements from list rhs
template<typename T>
List<T>::List(const List &rhs)
{
init();
for(auto & x:rhs)
push_back(x);
}
// Move constructor
template <typename T>
List<T>::List(List && rhs): theSize{rhs.theSize},head{rhs.head},tail{rhs.tail}
{
rhs.theSize=0;
rhs.head=nullptr;
rhs.tail=nullptr;
}
// Constructs a list with num elements all initialized with value val
template <typename T>
List<T>::List(int num, const T & val)
{
init();
int count;
for(count=0;count!=num;++count)
push_front(val);
}
// Construct a list with elements from another list between start and end [start, end)
template <typename T>
List<T>::List(const_iterator start, const_iterator end)
{
init();
for(const_iterator itr=start;itr!=end;++itr)
push_back(*itr);
}
// Destructor
template <typename T>
List<T>::~List()
{
clear();
delete head;
delete tail;
}
// Copy assignment operator
template <typename T>
const typename List<T>::List& List<T>::operator=(const List &rhs)
{
List copy=rhs;
std::swap(*this,copy);
return *this;
}
// Move assignment operator
template <typename T>
typename List<T>::List& List<T>::operator=(List && rhs)
{
std::swap(theSize, rhs.theSize);
std::swap(head, rhs.head);
std::swap(tail, rhs.tail);
return *this;
}
// Initiates the list
template <typename T>
void List<T>::init()
{
theSize=0;
head=new Node;
tail=new Node;
head->next=tail;
tail->prev=head;
}
// Returns the number of elements in the List
template <typename T>
int List<T>::size() const
{
return theSize;
}
// Returns if empty
template <typename T>
bool List<T>::empty() const
{
return size()==0;
}
// Deletes all the elements in the List
template <typename T>
void List<T>::clear()
{
while(!empty())
pop_front();
}
// Reverses the order of the elements in the list
template <typename T>
void List<T>::reverse()
{
List<T> revlist;
for(auto itr=--end();itr!=head;itr--)
{
revlist.push_back(*itr);
}
std::swap(head, revlist.head);
std::swap(tail, revlist.tail);
}
// Return reference to the first element in the List
template <typename T>
T& List<T>::front()
{
return *begin();
}
template <typename T>
const T& List<T>::front() const
{
return *begin();
}
// Return reference to the last element in the List
template <typename T>
T& List<T>::back()
{
return *--end();
}
template <typename T>
const T& List<T>::back() const
{
return *--end();
}
// Insert new object as the first
template <typename T>
void List<T>::push_front(const T & val)
{
insert(begin(),val);
}
// Insert new object as the first, move version
template <typename T>
void List<T>::push_front(T && val)
{
insert(begin(),std::move(val));
}
// Insert new object as the last
template <typename T>
void List<T>::push_back(const T & val)
{
insert(end(),val);
}
// Insert new object as the last, move version
template <typename T>
void List<T>::push_back(T && val)
{
insert(end(),std::move(val));
}
// Delete first element
template <typename T>
void List<T>::pop_front()
{
erase(begin());
}
// Delete last element
template <typename T>
void List<T>::pop_back()
{
erase(--end());
}
// Delete all nodes with value equal to val
template <typename T>
void List<T>::remove(const T &val)
{
for(auto itr=begin();itr!=end();)
{
if(*itr==val)
itr=erase(itr);
else
++itr;
}
}
// Print all elements in the List
template <typename T>
void List<T>::print(std::ostream &os, char ofc) const
{
Node * mon=head;
int i;
for(i=0;i<theSize;i++)
{
mon =mon->next;
os<<(mon->data)<<ofc;
}
}
// Return iterator to first element in the List
template <typename T>
typename List<T>::iterator List<T>::begin()
{
return {head->next};
}
template <typename T>
typename List<T>::const_iterator List<T>::begin() const
{
return {head->next};
}
// Return iterator to last element in the List
template <typename T>
typename List<T>::iterator List<T>::end()
{
return {tail};
}
template <typename T>
typename List<T>::const_iterator List<T>::end() const
{
return {tail};
}
// Insert value val ahead of the node
template <typename T>
typename List<T>::iterator List<T>::insert(List<T>::iterator itr, const T &val)
{
Node *p=itr.current;
theSize++;
return {p->prev=p->prev->next=new Node {val, p->prev,p}};
}
// Insert value val ahead of the node, move version
template <typename T>
typename List<T>::iterator List<T>::insert(List<T>::iterator itr, T && val)
{
Node *p=itr.current;
theSize++;
return {p->prev=p->prev->next=new Node {std::move(val),p->prev,p}};
}
// Delete node refferred to by itr
template <typename T>
typename List<T>::iterator List<T>::erase(List<T>::iterator itr)
{
Node *p=itr.current;
List<T>::iterator retVal{p->next};
p->prev->next=p->next;
p->next->prev=p->prev;
delete p;
theSize--;
return retVal;
}
// Delete all nodes between [start,end)
template <typename T>
typename List<T>::iterator List<T>::erase(List<T>::iterator start,List<T>::iterator end)
{
List<T>::iterator itr;
for(itr=start;itr!=end;)
itr=erase(itr);
return end;
}
//____________________ NON CLASS GLOBAL FUNCTIONS _________________________
// Check if two lists contain the same sequence of elements
template <typename T>
bool operator==(const List<T> & lhs, const List<T> & rhs)
{
if (lhs.size() != rhs.size())
return false;
auto left=lhs.begin();
auto right=rhs.begin();
while (left != lhs.end() && right != rhs.end())
{
if(*left!=*right)
return false;
left++;
right++;
}
return true;
}
// Opposite of equal
template <typename T>
bool operator!=(const List<T> & lhs, const List<T> & rhs)
{
return !(lhs==rhs);
}
// Print out all elemends in list l by calling List class print function
template <typename T>
std::ostream & operator<<(std::ostream & os, const List<T> &l)
{
l.print(os);
return os;
}