Define and show the implementation of the functions of an arrayList.

The ArrayList is a dynamic array implementation in C++ that allows for the insertion, deletion, and retrieval of elements at any position. Here is the implementation of the functions for an ArrayList in C++:

#include <iostream>
using namespace std;

class ArrayList {
private:
    int *arr;
    int capacity;
    int size;

public:
    ArrayList() {
        capacity = 10;
        size = 0;
        arr = new int[capacity];
    }

    ~ArrayList() {
        delete[] arr;
    }

    void resize() {
        int tempArr = new int[capacity  2];
        for (int i = 0; i < size; i++) {
            tempArr[i] = arr[i];
        }
        delete[] arr;
        arr = tempArr;
        capacity *= 2;
    }

    void add(int element) {
        if (size == capacity) {
            resize();
        }
        arr[size++] = element;
    }

    void insert(int element, int position) {
        if (size == capacity) {
            resize();
        }
        for (int i = size; i > position; i--) {
            arr[i] = arr[i - 1];
        }
        arr[position] = element;
        size++;
    }

    void remove(int position) {
        for (int i = position; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }
        size--;
    }

    int get(int position) {
        return arr[position];
    }

    int getSize() {
        return size;
    }

    void display() {
        for (int i = 0; i < size; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
};

int main() {
    ArrayList myList;
    myList.add(1);
    myList.add(2);
    myList.add(3);
    myList.insert(4, 1);
    myList.remove(2);
    myList.display();
    return 0;
}

Explanation of the functions:

  • ArrayList(): This is the constructor of the ArrayList class. It initializes the capacity and size variables, and creates a dynamic array of integers with an initial capacity of 10.

  • ~ArrayList(): This is the destructor of the ArrayList class. It frees the memory allocated for the dynamic array.

  • resize(): This function resizes the dynamic array by creating a new array with double the capacity, copying the elements from the old array to the new array, and updating the capacity variable.

  • add(int element): This function adds an element to the end of the ArrayList. If the size exceeds the capacity, the resize() function is called to increase the capacity.

  • insert(int element, int position): This function inserts an element at the specified position in the ArrayList. If the size exceeds the capacity, the resize() function is called. The existing elements after the specified position are shifted to make room for the new element.

  • remove(int position): This function removes the element at the specified position from the ArrayList. The elements after the specified position are shifted to fill the gap.

  • get(int position): This function returns the element at the specified position in the ArrayList.

  • getSize(): This function returns the current size of the ArrayList.

  • display(): This function displays the elements of the ArrayList.

The main function demonstrates the usage of the ArrayList class by creating an instance, adding elements, inserting an element, removing an element, and displaying the elements.