-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLList.cpp
316 lines (272 loc) · 8.64 KB
/
LList.cpp
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
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
// Slight modification by GOH KUN SHUN
/** @file Node.cpp
Listing 4-2 */
#include "LList.h"
#include "Node.h"
#include <stdlib.h>
#include <iostream>
#include <ctime>
template <class ObjectType>
LList<ObjectType>::LList()
: headPtr( nullptr ), objCount( 0 )
{
}
template <class ObjectType>
LList<ObjectType>::LList( const LList<ObjectType> &linkedList )
: objCount( linkedList.objCount ), sortDsc( linkedList.sortDsc )
{
Node<ObjectType>* origListPtr = linkedList.headPtr; // Points to nodes in original chain
// Check if original chain it empty
if ( origListPtr == nullptr )
headPtr = nullptr;
else
{
// Copy first node
headPtr = new Node<ObjectType>();
headPtr->setObject( origListPtr->getObject() );
// Copy remaining nodes
Node<ObjectType>* newListPtr = headPtr; // Points to last node in new chain
origListPtr = origListPtr->getNext(); // Advance original-chain pointer
while ( origListPtr != nullptr )
{
// Get next object from original chain
ObjectType nextObject = origListPtr->getObject();
// Create a new node containing the next object
Node<ObjectType>* newNodePtr = new Node<ObjectType>( nextObject );
// Link new node to end of new chain
newListPtr->setNext( newNodePtr );
// Advance pointer to new last node
newListPtr = newListPtr->getNext();
// Advance original-chain pointer
origListPtr = origListPtr->getNext();
}
newListPtr->setNext( nullptr );
}
}
template <class ObjectType>
LList<ObjectType>::~LList()
{
clear();
}
template <class ObjectType>
bool LList<ObjectType>::isEmpty()
{
return objCount == 0;
}
template <class ObjectType>
int LList<ObjectType>::getLength()
{
return objCount;
}
template <class ObjectType>
bool LList<ObjectType>::insert( ObjectType obj )
{
return insertAt( obj, objCount + 1 );
}
template <class ObjectType>
bool LList<ObjectType>::sortedInsertDesc( ObjectType obj )
{
// Sort the nodes
if ( !sortDsc )
sortDesc();
// Create new node to be insert
Node<ObjectType>* newNodePtr = new Node<ObjectType>( obj );
// If the objCount is 0 or it's smaller than the first node, insert the new item at the beginning
if ( objCount == 0 || obj > headPtr->getObject() )
{
// Point newNodePtr's nest to headPtr
newNodePtr->setNext( headPtr );
// set newNodePtr as the new headPtr
headPtr = newNodePtr;
// Update objCount
objCount++;
return true;
}
else
{
// Declare 2 pointers to allow new entry to be inserted in the middle
Node<ObjectType>* prevPtr = headPtr;
Node<ObjectType>* currentPtr;
// A counter to keep track of current item position
int i = 1;
// Check if there is only one item, if yes, put it at the end, otherwise loop until
// you find the suitable position to be inserted or objCount is reach
while ( i < objCount )
{
// Point current pointer to prevPtr's next
currentPtr = prevPtr->getNext();
if ( obj <= prevPtr->getObject() && obj >= currentPtr->getObject() )
{
// Connect prevPtr's next to newNodePtr
prevPtr->setNext( newNodePtr );
// Connect newNodePtr's next to currentPtr
newNodePtr->setNext( currentPtr );
// Update objCount
objCount++;
return true;
}
// Update counter
i++;
// Update prevPtr to it's next
prevPtr = prevPtr->getNext();
}
// Insert the new node at the end
// Set newnOdePtr's next to nullptr
newNodePtr->setNext( nullptr );
// Connect the previous last node's next ( prevPtr ) to new node
prevPtr->setNext( newNodePtr );
// Update objCount
objCount++;
return true;
}
}
template <class ObjectType>
bool LList<ObjectType>::erase( int pos )
{
bool ableToRemove = ( pos >= 1 ) && ( pos <= objCount );
if ( ableToRemove )
{
Node<ObjectType>* curPtr = nullptr;
if ( pos == 1 )
{
// Remove the first node in the chain
curPtr = headPtr; // Save pointer to node
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
Node<ObjectType>* prevPtr = getNodeAt( pos - 1 );
// Point to node to delete
curPtr = prevPtr->getNext();
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext( curPtr->getNext() );
}
// Return node to system
curPtr->setNext( nullptr );
delete curPtr;
curPtr = nullptr;
objCount--; // Decrease count of entries
}
return ableToRemove;
}
template <class ObjectType>
ObjectType LList<ObjectType>::retrieve( int pos )
{
// Enforce precondition
bool ableToGet = ( pos >= 1 ) && ( pos <= objCount );
if ( ableToGet )
return getNodeAt( pos )->getObject();
else
{
std::string message = "getEntry() called with an empty list or ";
message = message + "invalid position.";
throw( PrecondViolatedExcep( message ) );
}
}
template <class ObjectType>
void LList<ObjectType>::randomize()
{
// Seed random generator
srand( time( NULL ) );
int n = 0;
// Swap questions with from random generated number n
for ( unsigned int i = 1; i <= objCount; i++ )
{
n = rand() % objCount;
swapNodes( i, n );
}
sortDsc = false;
}
template <class ObjectType>
bool LList<ObjectType>::insertAt( ObjectType obj, int pos )
{
bool ableToInsert = ( pos >= 1 ) && ( pos <= objCount + 1 );
if ( ableToInsert )
{
// Create a new node containing the new entry
Node<ObjectType>* newNodePtr = new Node<ObjectType>( obj );
// Attach new node to chain
if ( pos == 1 )
{
// Insert new node at beginning of chain
newNodePtr->setNext( headPtr );
headPtr = newNodePtr;
}
else
{
// Find node that will be before new node
Node<ObjectType>* prevPtr = getNodeAt( pos - 1 );
// Insert new node after node to which prevPtr points
newNodePtr->setNext( prevPtr->getNext() );
prevPtr->setNext( newNodePtr );
}
objCount++; // Increase count of entries
sortDsc = false;
}
return ableToInsert;
}
template <class ObjectType>
Node<ObjectType>* LList<ObjectType>::getNodeAt( int pos ) const
{
// Debugging check of precondition
bool validPosition = ( ( pos >= 1 ) && ( pos <= objCount ) );
if ( validPosition )
{
// Count from the beginning of the chain
Node<ObjectType>* curPtr = headPtr;
for ( int skip = 1; skip < pos; skip++ )
curPtr = curPtr->getNext();
return curPtr;
}
else
return nullptr;
}
template <class ObjectType>
bool LList<ObjectType>::swapNodes( int pos1, int pos2 )
{
bool validSwap = ( ( pos1 >= 1 ) && ( pos1 <= objCount ) && ( pos2 >= 1 ) && ( pos2 <= objCount ) );
if ( validSwap )
{
// Create pointers that points to node to be swapped
Node<ObjectType>* node1 = getNodeAt( pos1 );
Node<ObjectType>* node2 = getNodeAt( pos2 );
// Swap the objects in the nodes
ObjectType temp = node1->getObject();
node1->setObject( node2->getObject() );
node2->setObject( temp );
}
return validSwap;
}
template <class ObjectType>
void LList<ObjectType>::clear()
{
while ( !isEmpty() )
erase( 1 );
}
template <class ObjectType>
ObjectType LList<ObjectType>::operator[]( int i )
{
return retrieve( i );
}
template <class ObjectType>
void LList<ObjectType>::sortDesc()
{
int max_index;
for ( int i = 1; i <= objCount; i++ )
{
max_index = i;
for ( int j = i; j <= objCount; j++ )
{
if ( getNodeAt( j )->getObject() > getNodeAt( max_index )->getObject() )
{
max_index = j;
}
}
swapNodes( i, max_index );
}
sortDsc = true;
}