Question: IN C++ Implement a list using an array. Requirements Files array_list.h - contains the template definitions array_list_tests.cpp - contains the test cases and test driver
IN C++
Implement a list using an array.
Requirements
Files
array_list.h - contains the template definitions
array_list_tests.cpp - contains the test cases and test driver (main)
Class
template
class ArrayList;
Functions (public)
ArrayList() - makes an empty list
explicit ArrayList(size_t) - makes a list with the specified initial capacity
Rule of Three
ArrayList(const ArrayList&) - constructs a copy of the given list
~ArrayList() - destroys this list
ArrayList& operator=(const ArrayList&) - assigns a copy of the given list
size_t size() - returns the number of elements in the list
Object& operator[](size_t) - returns a reference to the element at the specified index or throws std::out_of_range if the index is out of bounds.
void insert(size_t, const Object&) - insert the given object at the specified index or throws std::out_of_range if the index is out of bounds
void remove(index) - remove the object at the specified index or throws std::out_of_range if the index is out of bounds
Optional
ArrayList(ArrayList&&) - move-constructs a copy of the given (rvalue) list
ArrayList& operator=(ArrayList&&) - move-assigns a copy of the given (rvalue) list
void insert(size_t, Object&&) - insert the given (rvalue) object at the specified index or throws std::out_of_range if the index is out of bounds
const Object& operator[](size_t) const - returns a constant reference to the element at the specified index or throws std::out_of_range if the index is out of bounds.
Object* begin() - returns a pointer to the beginning of the list
const Object* begin() const - returns a pointer to the beginning of the list
Object* end() - returns a pointer to the end of the list
const Object* end() const - returns a pointer to the end of the list
Example
// make an empty list
ArrayList
// insert 3 values at the end of the list
list.insert(0, 1);
list.insert(1, 2);
list.insert(2, 3);
// get the size
size_t size = list.size();
// remove the middle element
list.remove(1);
// access the element at index 1
int value = list[1];
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
