-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyArrayListInt.cc
189 lines (169 loc) · 4.5 KB
/
MyArrayListInt.cc
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
//
// CS251
// Implementation of a Resizable Array for int
//
#include "MyArrayListInt.h"
#include <iostream>
using namespace std;
// Constructor to instantiate the array with a given capacity
MyArrayListInt::MyArrayListInt(int initialSize)
{
// Add implementation here
arrayList = new int[initialSize];
size = 0;
maxCapacity = initialSize;
}
// Add an int element to the end of the array and return true if successful
bool MyArrayListInt::addAtEnd(int value)
{
// Add implementation here
if(size == maxCapacity){
int * arrayListTemp = new int[maxCapacity * 2];
for(int i = 0; i<maxCapacity; i++){
arrayListTemp[i] = arrayList[i];
}
maxCapacity *= 2;
delete [] arrayList;
arrayList = arrayListTemp;
//arrayList = arrayListTemp;
/*arrayList = new int[maxCapacity *2];
maxCapacity *= 2;
for(int j = 0; j<maxCapacity; j++){
arrayList[j] = arrayListTemp[j];
}
delete [] arrayListTemp;*/
arrayList[size] = value;
size++;
return true;
}
else{
arrayList[size] = value;
size++;
return true;
}
return false;
}
// puts the int element at index in retval and returns true. returns false if no int element at given index
bool MyArrayListInt::elementAtIndex( int index, int & retval)
{
// Add implementation here
if(index < 0 || index >= size){
return false;
}
retval = arrayList[index];
return true;
}
// Add int element to index position in the array and return true if successful.
bool MyArrayListInt::addAtIndex(int index, int value)
{
// Add implementation here
if(index <0 || index > size){
return false;
}
else if(size == maxCapacity){
int * arrayListTemp = new int[maxCapacity *2 ];
for(int i = 0; i<maxCapacity; i++){
arrayListTemp[i] = arrayList[i];
}
maxCapacity *= 2;
delete [] arrayList;
arrayList = arrayListTemp;
/*arrayList = new int[maxCapacity *2];
//maxCapacity *= 2;
for(int j = 0; j<maxCapacity; j++){
arrayList[j] = arrayListTemp[j];
}
delete [] arrayListTemp;*/
for(int i = size -1; i>=index; i--){
arrayList[i+1] = arrayList[i];
}
arrayList[index] = value;
size++;
return true;
}
else{
for(int i = size - 1; i>=index; i--){
arrayList[i+1] = arrayList[i];
}
arrayList[index] = value;
size++;
return true;
}
return false;
}
// Remove the int element at index position in the array and return true if successful.
bool MyArrayListInt::removeAtIndex(int index)
{
// Add implementation here
if(index <0 || index >= size){
return false;
}
else{
for(int i = index +1; i<size; i++){
arrayList[i-1] = arrayList[i];
}
size--;
}
if(size <= (0.25 * maxCapacity)){
int * arrayListTemp = new int[maxCapacity / 2];
for(int i = 0; i<maxCapacity/2; i++){
arrayListTemp[i] = arrayList[i];
}
maxCapacity = maxCapacity / 2;
delete [] arrayList;
arrayList = arrayListTemp;
/*arrayList = new int[maxCapacity / 2];
maxCapacity = maxCapacity / 2;
for(int j = 0; j<maxCapacity; j++){
arrayList[j] = arrayListTemp[j];
}
delete [] arrayListTemp;*/
}
return true;
}
// return the current size of the array.
int MyArrayListInt::length()
{
// Add implementation here
return size;
}
// return the current capacity of the array .
int MyArrayListInt::capacity()
{
// Add implementation here
return maxCapacity;
}
// Substitute element at index and return true if successful
bool MyArrayListInt::replaceAtIndex(int index, int value)
{
// Add implementation here
if(index < 0 || index >= size){
return false;
}
arrayList[index] = value;
return true;
}
// Search for int and return true if element exists in the array
bool MyArrayListInt::find(int value)
{
// Add implementation here
for(int i = 0; i<size; i++){
if(arrayList[i] == value){
return true;
}
}
return false;
}
// Print all the int elements in the array
void MyArrayListInt::print()
{
cout<<"[";
int i;
for(i = 0; i<size-1; i++){
cout <<arrayList[i];
cout<<", ";
}
cout<<arrayList[i];
cout<<"]";
// Add implementation here
}