Array-Based Lists - Code
Array-Based Lists - Code
#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
bool arrayList::isFull()
{
return length == maxSize;
}
bool arrayList::isEmpty()
{
return length == 0;
}
int arrayList::listSize()
{
return length;
}
int arrayList::maxListSize()
{
return maxSize;
}
return -1;
}
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::addBouns(int b)
{
for (int i = 0; i < length; i++)
list[i] += b;
}
}
// ****************************************************************
// 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);
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);
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;
}