-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.hpp
336 lines (283 loc) · 8.31 KB
/
LinkedList.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
#ifndef _LINKEDLIST_HPP_
#define _LINKEDLIST_HPP_
#include <iostream>
using namespace std;
#include "Node.hpp"
#include "IncreasingOrderSort.hpp"
#include "UseNew.hpp"
#include "Exception.hpp"
namespace LinkedList {
/* This is a class built using Policy-based design. */
/* Using templates, the user can specify how s/he */
/* would like the class to allocate and de-allocate */
/* memory as well as how to insert nodes into the list */
template
<
class T,
template <class> class SortingPolicy = SortingPolicies::IncreasingOrderSort,
template <class> class AllocationPolicy = AllocationPolicies::UseNew
>
class LinkedList {
public:
/* default constructor, initialize all iterators */
explicit LinkedList() {
_head = NULL;
_begin.setStatus(iterator::BEGIN);
_begin.setIter(_head);
_end.setStatus(iterator::END);
} /* end LinkedList() */
/* copy constructor */
LinkedList( const LinkedList& other ) {
copy( other );
}
/* copy method to copy contents of linked list to another linked list */
void copy( const LinkedList& other ) {
clear();
Node<T> * node = other._head;
while( node != NULL )
{
insert( node->data() );
node = node->next();
}
_begin.setIter(_head);
}
/* deletes all nodes in the list */
virtual ~LinkedList() {
clear();
} /* end ~LinkedList() */
/* just a debugging method so I can see the list */
void printVisual() {
Node<T> * node = _head;
while( node != NULL )
{
if( node->prev() != NULL )
cout << "<-";
else
cout << "|-";
cout << node->data();
if( node->next() != NULL )
cout << "->";
else
cout << "-|";
node = node->next();
}
cout << endl;
}
/* return true is the list is empty */
bool isEmpty() const {
return (_head == NULL) ? true : false;
}
/* insert a value based on the SortingPolicy */
/* node memory is allocated based on the AllocationPolicy */
void insert( const T& val ) {
if( _head == NULL )
{
_head = AllocationPolicy<T>::newNode( val );
_begin.setIter(_head);
return;
}
Node<T> * node = _head;
if( SortingPolicy<T>::putBefore( val, node->data() ) )
{
Node<T> * newNode = AllocationPolicy<T>::newNode( val, _head );
_head->_prev = newNode;
_head = newNode;
_begin.setIter(_head);
return;
}
while( node->next() != NULL )
{
if( SortingPolicy<T>::putBefore( val, node->next()->data() ) )
{
Node<T> * newNode = AllocationPolicy<T>::newNode( val, node->next(), node ); /* new Node<T>( val, node->next() ); */
node->next()->_prev = newNode;
node->_next = newNode;
return;
}
node = node->next();
}
/* putting the node at the end of the list */
node->_next = AllocationPolicy<T>::newNode( val, NULL, node ); /* new Node<T>( val ); */
_begin.setIter( _head );
return;
}
/* returns a const ref to the node at position i (indexed from 0) */
const T& get( int index ) const {
if( isEmpty() ) throw Exception("Tried to access data in empty list");
if( index < 0 ) throw Exception("Tried to access with negative position");
Node<T> * node = _head;
for( int i = 0; i < index; ++i )
{
if( node == NULL ) throw Exception("Tried to access data beyond bounds of list");
node = node->next();
}
if( node == NULL ) throw Exception("Tried to access data beyond bounds of list");
else return node->data();
} /* end get() */
/* return the number of elements in the list */
/* not efficient because no counter is kept, */
/* so must iterate through entire list */
int size() {
Node<T> * n = _head;
int count = 0;
while( n != NULL )
{
n = n->_next;
count++;
}
return count;
}
/* same as size() */
int length() {
return size();
}
/************************************************/
/* Iterator class for the LinkedList class */
class iterator
{
public:
enum Status { NOP = 0, BEGIN, END, MIDDLE };
public:
explicit iterator() {
_iter = NULL;
_status = NOP;
}
iterator(const typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& it) {
_iter = it._iter;
_status = it._status;
}
virtual ~iterator() {
}
/* move the iterator forward one step */
void forward() {
if( _status == NOP )
return;
else if( _status == BEGIN ) {
_status = MIDDLE;
return;
}
else if( _status == END ) {
return;
}
else if( _iter->next() != NULL )
{
_iter = _iter->next();
}
else
{
_iter = NULL;
_status = END;
}
}
const T& getData() const {
return _iter->data();
}
Node<T> * getIter() const {
return _iter;
}
Node<T> * operator*() const {
return getIter();
}
void setIter(Node<T> * node) {
_iter = node;
_status = (node != NULL) ? MIDDLE : END;
}
void setStatus(Status status) {
_status = status;
}
/* test if the iterators are pointing to the same thing */
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& operator=( const typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& rhs ) {
if( this == &rhs )
return *this;
else
{
_iter = rhs._iter;
_status = rhs._status;
return *this;
}
}
bool operator==( const typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& rhs ) {
if( (_status == BEGIN) && (rhs._status == BEGIN) ) return true;
else if( (_status == END) && (rhs._status == END) ) return true;
else if( _iter == rhs._iter ) return true;
else return false;
}
bool operator!=( const typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& rhs ) {
return !(*this == rhs);
}
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator operator++() {
forward();
return *this;
}
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator operator++(int) {
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator t(*this); // save obj
forward();
return t;
}
/* same thing as getIter() and operator*() */
Node<T> * getNode() {
return _iter;
}
private:
Node<T> * _iter;
Status _status;
};
/************************************************/
/* returns the iterator to the beginning of the list */
const typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& begin() const {
return _begin;
}
/* returns the iterator to the end of the list */
const typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& end() const {
return _end;
}
/* removes a node given an iterator to the given node */
void remove( typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator& it ) {
Node<T> * node = it.getIter();
if( node == NULL ) return;
Node<T> * prev = node->prev();
Node<T> * next = node->next();
if( node == _head )
_head = next;
it.setIter( prev );
it.setStatus( ( it.getIter() == NULL ) ?
LinkedList<T, SortingPolicy, AllocationPolicy>::iterator::BEGIN :
LinkedList<T, SortingPolicy, AllocationPolicy>::iterator::MIDDLE);
if( prev != NULL )
prev->_next = next;
if( next != NULL )
next->_prev = prev;
AllocationPolicy<T>::removeNode( node );
_begin.setIter(_head);
}
/* traverses the list until the given index then passes the iterator to the other
remove function */
void remove( int index ) {
if( index < 0 ) return;
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator it = begin();
for( int i = 0; (i < index) && (it != end()); ++i)
it++;
remove(it);
}
/* remove all nodes from the list */
void clear() {
Node<T> * node = _head;
Node<T> * tmp;
while( node != NULL )
{
tmp = node->next();
AllocationPolicy<T>::removeNode( node );
node = tmp;
if( node != NULL )
node->_prev = NULL;
}
_head = NULL;
_begin.setIter(_head);
}
private:
Node<T> * _head;
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator _begin;
typename LinkedList<T, SortingPolicy, AllocationPolicy>::iterator _end;
};
}
#endif