-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleList.cpp
234 lines (186 loc) · 4.44 KB
/
SimpleList.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
//SimpleList.cpp
//Originally by Michael Leverington
//Implements SimpleList functions
//Modified for CS446 Assignment 2
//by Austin Bachman
//9/24/16
// Precompiler directives /////////////////////////////////////////////////////
#ifndef CLASS_SIMPLELIST_CPP
#define CLASS_SIMPLELIST_CPP
// Header files ///////////////////////////////////////////////////////////////
#include "SimpleList.h"
template <class DataType>
SimpleList<DataType>::SimpleList
(
int newCapacity
)
: listSize( 0 )
{
listCapacity = getMax( newCapacity, DEFAULT_CAPACITY );
listData = new DataType[ listCapacity ];
}
template <class DataType>
SimpleList<DataType>::SimpleList
(
const SimpleList<DataType> &copiedList
)
: listSize( copiedList.listSize ),
listCapacity( copiedList.listCapacity ),
listData( new DataType[ listCapacity ] )
{
copyList( listData, copiedList.listData, listCapacity );
}
template <class DataType>
SimpleList<DataType>::~SimpleList
(
// no parameters
)
{
delete [] listData;
}
template <class DataType>
const SimpleList<DataType> &SimpleList<DataType>::operator =
(
const SimpleList<DataType> & rhList
)
{
if( this != &rhList )
{
delete [] listData;
listCapacity = rhList.listCapacity;
listSize = rhList.listSize;
listData = new DataType [ listCapacity ];
copyList( listData, rhList.listData, listSize );
}
return *this;
}
template <class DataType>
DataType &SimpleList<DataType>::operator [ ]
(
int index
)
{
return listData[ index ];
}
template <class DataType>
const DataType &SimpleList<DataType>::operator [ ]
(
int index
) const
{
return listData[ index ];
}
template <class DataType>
int SimpleList<DataType>::getSize
(
// no parameters
) const
{
return listSize;
}
template <class DataType>
int SimpleList<DataType>::getCapacity
(
// no parameters
) const
{
return listCapacity;
}
template <class DataType>
void SimpleList<DataType>::clear
(
// no parameters
)
{
listSize = 0;
}
template <class DataType>
void SimpleList<DataType>::addItem
(
const DataType &item
)
{
listData[ listSize ] = item;
listSize++;
}
template <class DataType>
bool SimpleList<DataType>::findData
(
DataType &item,
bool removeFlag
)
{
int index;
bool found = false;
for( index = 0; index < listSize && !found; index++ )
{
if( item.compareTo( listData[ index ] ) == 0 )
{
getAtIndex( index, item, removeFlag );
found = true;
}
}
return found;
}
template <class DataType>
bool SimpleList<DataType>::resize
(
int newCapacity
)
{
DataType *newList;
if( newCapacity > listCapacity )
{
newList = new DataType[ newCapacity ];
copyList( newList, listData, listCapacity );
delete [] listData;
listData = newList;
listCapacity = newCapacity;
return true;
}
return false;
}
template <class DataType>
void SimpleList<DataType>::getAtIndex
(
int index,
DataType &foundData,
bool removeFlag
)
{
foundData = listData[ index ];
if( removeFlag )
{
listSize--;
while( index < listSize )
{
listData[ index ] = listData[ index + 1 ];
index++;
}
}
}
template <class DataType>
void SimpleList<DataType>::copyList
(
DataType *dest,
const DataType *source,
int copyLength
)
{
int index;
for( index = 0; index < copyLength; index++ )
{
dest[ index ] = source[ index ];
}
}
template <class DataType>
int SimpleList<DataType>::getMax( int one, int other )
{
if( other > one )
{
return other;
}
return one;
}
// Terminating precompiler directives ////////////////////////////////////////
#endif // #ifndef CLASS_SIMPLELIST_CPP