#include <iostream>
#include <cassert>
using namespace std;
class arrayList {
private:
int* list;
int length;
int maxSize;
public:
arrayList(int); // constructor
arrayList(arrayList&); // copy constructor
~arrayList(); // destructor
bool isFull();
bool isEmpty();
int listSize();
int maxListSize();
void insertEnd(int item);
void insertAt(int pos, int item);
void insert(int item); //with no duplicate (not important to implement)
void removeAt(int pos);
void remove(int item);
int seqSearch(int item); // sequential search
void print();
void retrieveAt(int, int&); // getter from location (because it pointer)
void replaceAt(int, int);
void clear();
bool isItemEqual(int, int);
void addBouns(int b);
void removeAll(int item); // delete all item are same in the list
};
arrayList::arrayList(int s) //constructor
{
/* initilize the private members*/
if (s > 0)
maxSize = s;
else
maxSize = 100; // default size if the user not eter array size
length = 0; // list should when start be empty
list = new int[maxSize]; // array initialised
assert(list != NULL); // terminate if unable to allocate memory space
} //stop the progarm and exsit
arrayList::arrayList(arrayList &other) //copy constructor
// to avoid shallow copy
{
length = other.length;
maxSize = other.maxSize;
//list = other.list; // to avoid the shallow copy we not make this
list = new int[maxSize]; // create new array list
for (int i = 0; i < length; i++) // copy otherList
list[i] = other.list[i];
}
arrayList::~arrayList()
{
delete[]list;
}
bool arrayList::isFull()
{
return length == maxSize;
}
bool arrayList::isEmpty()
{
return length == 0;
}
int arrayList::listSize()
{
return length;
}
int arrayList::maxListSize()
{
return maxSize;
}
void arrayList::insertEnd(int item) //insert at the end of the list
{
if ( isFull() )
cout << "List is Full" << endl;
else
list[length++] = item;
}
void arrayList::insertAt(int pos, int item)
{
if (isFull())
cout << "List is Full" << endl;
else
if (pos >= 0 && pos <= length)
{
for (int i = length; i > pos; i--)
list[i] = list[i - 1]; // shifting method
list[pos] = item;
length++;
}
else
cout << "out of rang" << endl;
}
void arrayList::insert(int item)
{
int loc = seqSearch(item);
if (loc == -1)
insertEnd(item);
else
cout << "Item in the list" << endl;
}
void arrayList::removeAt(int pos)
{
if (isEmpty())
cout << "List is empty" << endl;
else
if (pos < 0 || pos >= length)
cout << "out of rang";
else
{
for (int i = pos; i < length - 1; i++)
list[i] = list[i + 1];
}
}
int arrayList::seqSearch(int item)
{
for (int i = 0; i < length; i++)
if (list[i] == item)
return i;
return -1;
}
void arrayList::remove(int item)
{
int loc = seqSearch(item);
if (loc == -1)
cout << "Item not in the list" << endl;
else
removeAt(loc);
}
void arrayList::print()
{
if (isEmpty())
cout << "list is Empty" << endl;
else
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
}
void arrayList::retrieveAt(int loc, int& item)
{
if (loc >= 0 && loc < length)
item = list[loc];
else
cout << "out of rang location" << endl;
}
void arrayList::replaceAt(int loc, int item)
{
if (loc < 0 || loc >= length)
cout << "Out of rang location" << endl;
else
list[loc] = item;
}
void arrayList::clear()
{
length = 0;
}
bool arrayList::isItemEqual(int loc, int item)
{
return (list[loc] == item);
}
void arrayList::addBouns(int b)
{
for (int i = 0; i < length; i++)
list[i] += b;
}
void arrayList::removeAll(int item)
{
int loc = seqSearch(item);
while( loc != -1)
{
removeAt(loc);
loc = seqSearch(item);
}
}
// ****************************************************************
// NON-MEMBER Functions
// ****************************************************************
// Example: Non-member funcation to add value to all even numbers in the list
void addEven(arrayList& list1, int val)
{
//for ( int i = 0; i < length; i++) // wrong, bcause need to tell to which calss
//for ( int i = 0; i < list.length; i++) // wrong, bcause length is pravite
int item;
for (int i = 0; i < list1.listSize(); i++) // we need list size of array (is
length)
{
//list1.listSiz[i]; // because we declare list as private data member
list1.retrieveAt(i, item); // location i save it at parameter item
if (item % 2 == 0) // is even
list1.replaceAt(i, item + val); // edit each item in the list
}
}
// Example: delete the occurrence ()اي عنصر يظهر في القائمة
void removeAll(arrayList &list1, int item) // can be the same name with the function
in the class
{
int loc = list1.seqSearch(item);
while (loc != -1)
{
list1.removeAt(loc);
loc = list1.seqSearch(item);
}
int main()
{
//arrayList list3(); // create any object means call the constructor
//arrayList list1(20);
//arrayList list2(list1);
arrayList list1(20);
list1.insertEnd(10);
list1.insertEnd(20);
list1.insertEnd(20);
list1.insertAt(1, 40); // location 1 add 40
list1.insertEnd(40);
list1.print();
cout << list1.seqSearch(40) << endl;
int x;
list1.retrieveAt(2, x);
cout << x << endl;
list1.removeAll(40);
//list1.remove(40); // well delete the only first 40
//removeAll(list1, 40); //calling Non-member function
list1.print();
cout << list1.listSize() << endl; // length of elemnet in the list
cout << list1.maxListSize(); //the list size
return 0;
}