Implement the function below. void swap(int pos1, int pos2){}
Implement the function below.
void swap(int pos1, int pos2){}
Initial code to be completed:
class ArrayList : public List {
int* array;
int index;
int capacity;
void dyn_all_add(){
int cap = ceil(capacity * 1.5);
array = (int*)realloc(array,cap * sizeof(int));
capacity = cap;
}
void dyn_all_rem(){
int cap = capacity - (capacity/3);
array = (int*)realloc(array,cap * sizeof(int));
capacity = cap;
}
public:
// CONSTRUCTOR
ArrayList() {
capacity = 4;
array = (int*)malloc(capacity);
index = 0;
}
int add(int num) {
if (index == capacity){
dyn_all_add();
}
*(array + index) = num;
index++;
return index;
}
int get(int pos){
if (pos-1 < index){
return *(array + pos-1);
}
return -1;
}
int size(){
return index;
}
void swap(int pos1, int pos2){
}
// WARNING! Do not modify the print method.
// Doing so will nullify your score for this activity.
void print() {
cout << "[";
for (int i = 0; i < capacity; i++) {
if (i < index) {
cout << *(array + i);
} else {
cout << "?";
}
if (i != capacity - 1) {
cout << ", ";
}
}
cout << "]" << endl;
}
};
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images