Question
Please finish implementation in c++ for del (delete function) and main function. // Arraylist.cpp #include ArrayList.h #include ArrayList::ArrayList() { _pa = new int{10}; _size =
Please finish implementation in c++ for del (delete function) and main function.
// Arraylist.cpp #include "ArrayList.h" #include
return*this; } ArrayList::~ArrayList() { delete[] _pa; } void ArrayList::push_back(int num) { add(num); } int ArrayList::pop() { if (_size <= 0) { std::cerr << "Fail to pop().Arraylist empty. "; exit(1); } int temp = _pa[_size - 1]; //element is deleted from arraylist since we have size to guide acc _size--; return temp; } int ArrayList::get(int index) { if (index < 0 || index >= _size) { std::cerr << "Fail to get().invaild index. "; exit(1); } return _pa[index]; } void ArrayList::set(int index, int num) { if (index < 0 || index >= _size) { std::cerr << "Fail to set().Invalid index. "; exit(1); } _pa[index] = num; }
void ArrayList::add(int index, int num) { if (index<0 || index>_size) { std::cerr << "Fail to add().Invalid index. "; exit(1); } if (!_isFull()) { for (int i = _size - 1; i >= index; --i) { _pa[i + 1] = _pa[i]; } _pa[index] = num; _size++; } else { _doubleCapacity(); add(index, num); } } void ArrayList::del(int index) {
} 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; } //_before a function means its private within the class bool ArrayList::_isFull(){
return _size == _capacity; } void ArrayList::_doubleCapacity() { int* temp = new int[_capacity * 2]; for (int i = 0; i < _size; i++) { temp[i] = _pa[i]; } _capacity *= 2; delete[] _pa; _pa = temp; }
void ArrayList::add(int num) { if (!_isFull()) { _pa[_size] = num; _size++; } else { _doubleCapacity(); add(num); } }
int main() {
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started