An array list is one of the most important data structures used in many applications
#include <iostream>
using namespace std;
//Functions of ArrayLists:
class ArrayList {
public:
~ArrayList();
private:
// Some declarations for implementations of the above functions:
int data;
int item_andex;
public:
ArrayList doubleCapacity(int *x, int y);
ArrayList halfCapacity(int *x, int y);
private:
ArrayList arr_List;
// Implementations of the functions of ArrayList
public:
ArrayList() {
data = 0;
item_andex = 0;
*arr_List = new int[data];
}
// Insertion function of ArrayList
void add(int item) {
arr_List[item_andex] = item;
data++;
item_andex++;
if (data == item_andex) {
arr_List = doubleCapacity(arr_List, data);
}
}
//Deleting function of ArrayList
int deleteLast() {
int count = 0;
if (arr_List != 0) {
count = arr_List[size];
arr_List[size] = NULL;
if (item_andex < item_andex * 2) {
arr_List = halfCapacity(arr_List, item_andex);
}
} else {
cout << "Array is empty\n";
}
return count;
}
// ArrayList Size() function
int size() {
int count = 0;
for (int x = 0; x < arr_List.length; x++) {
if (arr_List[x] != NULL)
count++;
}
return count;
}
};
Comments
Leave a comment