-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintArray_t.cpp
More file actions
302 lines (209 loc) · 4.59 KB
/
intArray_t.cpp
File metadata and controls
302 lines (209 loc) · 4.59 KB
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
/*
* intArray_t.cpp
* Created on: 07/03/2011
*
* Assumption given at course forum:
* 1. same pointer will not be insert twice
* 2. avoid handle "new" failures
*/
#include <iostream>
#include "intArray_t.h"
using namespace std;
/**
* Constructors & Destructor
*/
/*
* Default Ctor
*/
intArray_t::intArray_t() {
size = 0;
initializeMembers(XPAND_VALUE);
}
/*
* Overload Ctor
*/
intArray_t::intArray_t(int initialSize)
: size(initialSize){
initializeMembers(size);
}
/*
* Dtor
*/
intArray_t::~intArray_t() {
delete[] arr;
}
/**
* Public members
*/
int intArray_t::getSize() const{
return this->size;
}
int intArray_t::getCapacity() const{
return this->capacity;
}
void intArray_t::insert(int* e){
insert_p(size, e); //insert element
}
int* intArray_t::getFirst() const{
if (this->size == 0) //Array is empty
return 0;
else
return this->arr[0];
}
int* intArray_t::getLast() const{
if (this->size == 0) //Array is empty
return 0;
else
return this->arr[size - 1];
}
int* intArray_t::find(int v){
int i = findIndex(0, v);
if (i != -1){
return this->arr[i];
}
else
return 0;
}
int* intArray_t::remove(int v){
return remove_p(v);
}
void intArray_t::removeAll(){
this->size = 0;
}
void intArray_t::removeAndDelete(int v){
int *e;
e = remove_p(v); //Find & remove first match
while (e != 0){
delete e; //Free memory
e = remove_p(v); //Remove next match
}
}
void intArray_t::removeAndDeleteAll(){
//free all elemets in array
for (int i = 0; i < this->size; i++){
delete this->arr[i];
}
this->size = 0;
}
int intArray_t::append(int i, int* e){
return insert_p(i + 1, e);
}
int intArray_t::prepend(int i, int* e){
return insert_p(i, e);
}
/**
* Private members
*/
/*
* Shift all elements that are on the right hand side of index i into index i
* Return 1 on success 0 otherwise
*/
int intArray_t::shiftLeft(int i){
if (i > this->size){
return 0;
}
for (int j = i; j < this->size - 1; j++){
this->arr[j] = this->arr[j + 1];
}
return 1;
}
/*
* Shift all elements on the right hand side of index i (including i) one index right
* Return 1 on success 0 otherwise
*/
int intArray_t::shiftRight(int i){
if (i > this->size || this->size + 1 > this->capacity){
return 0;
}
for (int j = size; j > i; j--){
this->arr[j] = this->arr[j - 1];
}
return 1;
}
/*
* Expand Array by xpand_value
* Return 1 on success 0 otherwise
*/
int intArray_t::xpand(){
int **tmp = new int* [this->capacity + this->xpand_value];
if (tmp == 0){
return 0;
}
for (int i = 0; i < this->size; i++){
tmp[i] = this->arr[i];
}
this->capacity += this->xpand_value;
delete[] arr;
this->arr = tmp;
return 1;
}
/*
* Insert element e at index i
* Return 1 on success 0 otherwise
*/
int intArray_t::insert_p(int i, int* newElement){
if( newElement == NULL){ //case NULL do nothing
return 0;
}
if (i > this->size){ //Attempt to insert after end of array
return 0;
}
if((this->size == this->capacity) && !xpand()){ //xpand if necessary. Return 0 on fail
return 0;
}
if (i < this->size && !shiftRight(i)){ //Move all elmements on the right hand size of i one position right
return 0;
}
this->size++; //increment size
this->arr[i] = newElement; //assign newElement
return 1;
}
/*
* Remove the first element with the value "value"
* return 0 if no such element, or the element otherwise
*/
int* intArray_t::remove_p(int value) {
int index = findIndex(0, value); //find the index of value
if (index == -1) //case no such element
{
return 0;
}
int* element = this->arr[index];
shiftLeft(index); //shift the elements in array left onto index i
this->size--; //decrease size by 1
return element;
}
/*
* Return the first index of the element with value "value" starting from index i (including i)
* Return index of element on success -1 otherwise (Not found)
*/
int intArray_t::findIndex(int startIndex, int value) {
for (int i = startIndex; i < this->size; i++) //search the element in the array
{
if (*(this->arr[i]) == value) {
return i;
}
}
return -1; //case element wasn't found
}
/*
* Initialize class members
*/
void intArray_t::initializeMembers(int initialCapacity) {
this->capacity = initialCapacity;
this->xpand_value = XPAND_VALUE;
this->arr = new int*[capacity];
}
/*
* Print out array status and elements
*/
ostream& operator<< (ostream& os, intArray_t& a){
os << "\n" << "Size: " << a.size << "\n" << "Capacity: "
<< a.capacity << "\n";
os << "| ";
for (int i = 0; i < a.size; i++) {
os << *(a.arr[i]) << " | ";
}
os << "\n";
return os;
}