-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.h
314 lines (267 loc) · 7.85 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
/////////////////////////////////////////////////////////////////
// Class definition file: LinkedList.h //
// //
// This file defines the LinkedList class template, a regular //
// doubly-linked circular list of nodes containing values of //
// type E (the template's parameterized data-type). Its //
// member functions include constructors, a destructor, and //
// standard isEmpty, getHeadValue, and getSize functions. //
// Insertion and removal always occur at the head of the list. //
/////////////////////////////////////////////////////////////////
#ifndef LINKED_LIST_H
#include <stdlib.h>
#include <assert.h>
////////////////////////////////////////////////////////
// DECLARATION SECTION FOR LINKED LIST CLASS TEMPLATE //
////////////////////////////////////////////////////////
template <class E> class LinkedList
{
public:
// Class constructors and destructor
LinkedList();
LinkedList(const LinkedList<E> &list);
~LinkedList();
// Member functions
bool isEmpty();
void insert(E item);
bool removeHead();
E getHeadValue();
E getHeadNextValue();
int getSize();
LinkedList<E>& operator ++ ();
LinkedList<E>& operator -- ();
void each( void (*f)( E item ), bool each_in_order = true );
void each( void (*f)( E item, int i ), bool each_in_order = true );
void each( bool (*f)( E item ), bool each_in_order = true );
void each( bool (*f)( E item, int i ), bool each_in_order = true );
protected:
// Data members
struct node;
typedef node *nodePtr;
struct node
{
E data;
nodePtr next;
nodePtr previous;
};
nodePtr head;
int size;
// Member function
void* getNode(E item);
};
///////////////////////////////////////////////
// IMPLEMENTATION SECTION FOR CLASS TEMPLATE //
///////////////////////////////////////////////
//////////////////////////////////////////////
// Default constructor: Sets up empty list. //
//////////////////////////////////////////////
template <class E>
LinkedList<E>::LinkedList()
{
head = NULL;
size = 0;
}
////////////////////////////////////////////////
// Copy constructor: Makes deep copy of list. //
////////////////////////////////////////////////
template <class E>
LinkedList<E>::LinkedList(const LinkedList<E> &list)
{
nodePtr copyPreviousPtr, copyCurrentPtr, origCurrentPtr;
size = list.size;
if (list.head == NULL)
head = NULL;
else
{
head = (nodePtr)getNode(list.head->data);
copyPreviousPtr = head;
origCurrentPtr = list.head->next;
while (origCurrentPtr != list.head)
{
copyCurrentPtr = (nodePtr)getNode(origCurrentPtr->data);
copyPreviousPtr->next = copyCurrentPtr;
copyCurrentPtr->previous = copyPreviousPtr;
copyPreviousPtr = copyCurrentPtr;
origCurrentPtr = origCurrentPtr->next;
}
copyPreviousPtr->next = head;
head->previous = copyPreviousPtr;
}
}
/////////////////////////////////////////////////////////////
// Destructor: Converts entire list back into free memory. //
/////////////////////////////////////////////////////////////
template <class E>
LinkedList<E>::~LinkedList()
{
nodePtr ptr;
while (head != NULL)
{
ptr = head;
if (head == head->next)
head = NULL;
else
{
head = head->next;
head->previous = ptr->previous;
head->previous->next = head;
}
delete ptr;
}
}
//////////////////////////////////////////////////////
// Function to determine whether the list is empty. //
//////////////////////////////////////////////////////
template <class E>
bool LinkedList<E>::isEmpty()
{
return (size == 0);
}
//////////////////////////////////////////////////////////////
// Function to insert value "item" at the head of the list. //
//////////////////////////////////////////////////////////////
template <class E>
void LinkedList<E>::insert(E item)
{
nodePtr insertPtr;
size++;
insertPtr = (nodePtr)getNode(item);
if (head == NULL)
{
head = insertPtr;
head->next = head;
head->previous = head;
}
else
{
insertPtr->next = head;
insertPtr->previous = head->previous;
head->previous->next = insertPtr;
head->previous = insertPtr;
head = insertPtr;
}
return;
}
//////////////////////////////////////////////////
// Function to remove the head element from //
// the list. A boolean is returned to indicate //
// whether such an element existed. //
//////////////////////////////////////////////////
template <class E>
bool LinkedList<E>::removeHead()
{
nodePtr currentPtr;
if ( ( size == 0) || (head == NULL) )
return false;
else
{
currentPtr = head;
if (head->next == head)
{
size = 0;
head = NULL;
}
else
{
size--;
head = currentPtr->next;
head->previous = currentPtr->previous;
currentPtr->previous->next = head;
}
delete currentPtr;
return true;
}
}
////////////////////////////////////////////////////////////////////
// Function getHeadValue returns a copy of the head node's value. //
////////////////////////////////////////////////////////////////////
template <class E>
E LinkedList<E>::getHeadValue()
{
assert(head != NULL);
return head->data;
}
/////////////////////////////////////////////////
// Function getHeadValue returns a copy of the //
// value of the node after the head node. //
/////////////////////////////////////////////////
template <class E>
E LinkedList<E>::getHeadNextValue()
{
assert(head != NULL);
return head->next->data;
}
/////////////////////////////////////////
// Function size returns the current //
// number of nodes in the linked list. //
/////////////////////////////////////////
template <class E>
int LinkedList<E>::getSize()
{
return size;
}
////////////////////////////////////////////////////////////
// The increment operator moves the head pointer one item //
// further down the linked list (if that's possible). //
////////////////////////////////////////////////////////////
template <class E>
LinkedList<E>& LinkedList<E>::operator ++ ()
{
if (head != NULL)
head = head->next;
return *this;
}
template <class E>
LinkedList<E>& LinkedList<E>::operator -- ()
{
if (head != NULL)
head = head->previous;
return *this;
}
//////////////////////////////////////////////////////////////////
// Function to generate a new node with the data value provided //
// in parameter item, and returning a pointer to this new node. //
//////////////////////////////////////////////////////////////////
template <class E>
void* LinkedList<E>::getNode(E item)
{
nodePtr temp = new node;
assert(temp != NULL);
temp->data = item;
temp->next = NULL;
temp->previous = NULL;
return temp;
}
//////////////////////////////////////////////////////////////////
// Function to loop through all of the items in the linked list //
// and then call a passed function with an item parameter. //
//////////////////////////////////////////////////////////////////
template <class E>
void LinkedList<E>::each( void (*f)( E item ), bool each_in_order )
{
for( int i = 0; i < this->size; i++, each_in_order ? this->operator++() : this->operator--() )
f( this->getHeadValue() );
}
template <class E>
void LinkedList<E>::each( void (*f)( E item, int i ), bool each_in_order )
{
for( int i = 0; i < this->size; i++, each_in_order ? this->operator++() : this->operator--() )
f( this->getHeadValue(), i );
}
template <class E>
void LinkedList<E>::each( bool (*f)( E item ), bool each_in_order )
{
bool keep_going = f( this->getHeadValue() );
for( int i = 1; i < this->size && keep_going; i++, each_in_order ? this->operator++() : this->operator--() )
keep_going = f( this->getHeadValue() );
}
template <class E>
void LinkedList<E>::each( bool (*f)( E item, int i ), bool each_in_order )
{
int i = 0;
bool keep_going = f( this->getHeadValue(), i );
for( i = 1; i < this->size && keep_going; i++, each_in_order ? this->operator++() : this->operator--() )
keep_going = f( this->getHeadValue(), i );
}
#define LINKED_LIST_H
#endif