-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayList.cpp
185 lines (147 loc) · 2.96 KB
/
ArrayList.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
//Joel Asante
//includes
#include "ArrayList.h"
#include <iostream>
//Constructor
ArrayList::ArrayList() {
_pa = new Car[10];
_size = 0;
_capacity = 10;
}
//p. COntrustor
ArrayList::ArrayList(int capacity) {
if (capacity > 0) {
_pa = new Car[capacity];
_size = 0;
_capacity = capacity;
}
else {
ArrayList();
}
}
ArrayList::ArrayList(const ArrayList& that) {
this->_capacity = that._capacity;
this->_size = that._size;
// this->_pa = that._pa; // shallow copy
this->_pa = new Car[_capacity];
for (int i = 0; i < _size; ++i) {
this->_pa[i] = that._pa[i];
}
}
ArrayList& ArrayList::operator=(const ArrayList& that) {
if (this != &that){
Car* temp = _pa;
this->_capacity = that._capacity;
this->_size = that._size;
this->_pa = new Car[_capacity];
for (int i = 0; i < _size; ++i) {
this->_pa[i] = that._pa[i];
}
delete[] temp;
}
return *this;
}
ArrayList::~ArrayList() {
delete[] _pa;
}
void ArrayList::push_back(Car obj) {
add(obj);
}
Car ArrayList::pop() {
if (_size <= 0) {
std::cerr << "Fail to exec pop(). ArrayList empty.";
exit(1);
}
Car temp = _pa[_size - 1];
_size--;
return temp;
}
Car ArrayList::get(int index) {
if (index < 0 || index >= _size) {
std::cerr << "Fail to exec get(). Invalid index.";
exit(1);
}
return _pa[index];
}
void ArrayList::set(int index, Car obj) {
if (index < 0 || index >= _size) {
std::cerr << "Fail to exec set(). Invalid index.";
exit(1);
}
_pa[index] = obj;
}
void ArrayList::add(int index, Car obj) {
// invalid index, call exit()
if (index < 0 || index > _size) {
std::cerr << "Invalid index. " << std::endl;
exit(1);
}
// valid index, add new element
if (!_isFull()) {
// case 1: add and shift
// case 2: add without shifting
// shift 0 or more elements
for (int i = _size - 1; i >= index; --i) { // note that (_size - 1)
_pa[i + 1] = _pa[i];
}
// set new element
_pa[index] = obj;
// manage _size
_size++;
}
else {
_doubleCapacity();
add(index, obj);
}
}
void ArrayList::add(Car obj) {
if (!_isFull()) {
_pa[_size] = obj;
// manage _size
_size++;
}
else {
_doubleCapacity();
add(obj);
}
}
void ArrayList::del(int index) {
// TO_DO: implement this function
if (index >= 0 && index < _size){
for(int i = index; i < _size; i++)
{
_pa[i] = _pa[i+1]; //Moving values from right to left
}
}
else {
cout << " Invaild , out oF RANGE";
}
}
int ArrayList::size() {
return _size;
}
int ArrayList::capacity() {
return _capacity;
}
void ArrayList::traverse() {
for (int i = 0; i < _size; ++i) {
std::cout << _pa[i] << " ";
}
std::cout << std::endl;
}
bool ArrayList::_isFull() {
return _size == _capacity;
}
void ArrayList::_doubleCapacity() {
// allocate new space with doubled capacity
Car* temp = new Car[_capacity * 2];
// copy existing elements over
for (int i = 0; i < _size; ++i) {
temp[i] = _pa[i];
}
// update _capacity
_capacity *= 2;
// update _pa
delete[] _pa;
_pa = temp;
}