Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 ArrayList::ArrayList() { _pa = new int{10}; _size = 0; _capacity = 10; } ArrayList::ArrayList(int capacity) { if (capacity > 0) { _pa = new int[capacity]; _size = 0; _capacity = capacity; } else { // call default constructor ArrayList(); } } ArrayList::ArrayList(const ArrayList& that) { this->_capacity = that._capacity; this->_size = that._size; //same as _size=that.size; //this->_pa=that._pa; shallow copy this->_pa = new int[_capacity]; for (int i = 0; i < _size; ++i) { this->_pa[i] = that._pa[i]; } } ArrayList& ArrayList::operator=(const ArrayList & that) { if (this != &that) { int* temp = this->_pa; this->_capacity = that._capacity; this->_size = that._size; this->_pa = new int[_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(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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions