In this assignment you are to create two Template classes that implement an Arra
ID: 3678736 • Letter: I
Question
In this assignment you are to create two Template classes that implement an ArrayList.These classes will derive from an Abstract ArrayList Class. Your derived classes will be named ArrayListLL and ArrayListArr ArrayListLL Class Your ArrayListLL class will use a doubly-linked-list structure to hold all elements of the list. You should maintain a head and tail pointerto your underlying list. ArrayListArr Class Your ArrayListArr class will use an array to hold all ist elements. The default size of your underlying array should be 16, unless otherwise specified in a constructor. Virtual Functions Your base ArrayList class should have the following pure virtual functions that are implemented in your derived classes: constructor() This constructor will create an ArrayList object. For ArrayListLL, your constructor should initialize head and tail to null. For ArrayListArr,you should instantiate an underlying array of 16 elements. constructor(int capacity) This constructor applies to the ArrayListArr class. It will create an array with the size capacity int size); - This function will return the number of elements in your ArrayList. int capacity); - This function will return the total capacity of your ArrayList object. Forthe ArrayListLLExplanation / Answer
Answer:
Note: With provided time able to complete ArrayListLL class alone.
#include <iostream>
using namespace std;
template <typename T>
class ArrayList
{
public:
ArrayList();
virtual int size()=0;
virtual int capacity()=0;
virtual void add(T el)=0;
virtual void add(T el, int index)=0;
virtual T operator[](int index)=0;
virtual T remove(int index)=0;
};
template <typename T>
class ArrayListLL:public ArrayList<T>
{
struct arrayListNode
{
T node_data;
arrayListNode* prevListNode;
arrayListNode* nextListNode;
arrayListNode(T t, arrayListNode* p, arrayListNode* n) : node_data(t), prevListNode(p), nextListNode(n) {}
};
arrayListNode* head;
arrayListNode* tail;
int s;
public:
ArrayListLL() : head( NULL ), tail ( NULL ) {s=0;}
template <typename T>
int size()
{
return s;
}
template<typename T>
T operator[](int index)
{
if(index>size())
return 0;
arrayListNode* t1=head;
while(index>0)
{
t=t1;
t1=t1->nextListNode;
index--;
}
return t1->node_data;
}
template <typename T>
void add(T el)
{
head = new arrayListNode(el, NULL, head);
if( head->nextListNode )
head->nextListNode->prevListNode = head;
if( !head||!tail)
tail = head;
s++;
}
template<typename T>
void add(T el, int index)
{
if(index==0)
{
head=new arrayListNode(el,head,NULL);
if(tail==NULL)
tail=head;
}
if(index==size()-1)
{
tail=new arrayListNode(el,NULL,tail);
if(head==NULL)
{
head=tail;
}
}
arrayListNode* t;
arrayListNode* t1=head;
while(index>0)
{
t=t1;
t1=t1->nextListNode;
index--;
}
arrayListNode* temp=new arrayListNode(el,t1,t);
t->nextListNode=temp;
t1->prevListNode=temp;
s++;
}
template<typename T>
T remove(int index)
{
if( !head||!tail )
throw("ArrayListLL : list empty");
if(index==0)
{
arrayListNode* temp(head);
T node_data( head->node_data );
head = head->nextListNode ;
if( head )
head->prevListNode = NULL;
else
tail = NULL;
delete temp;
s--;
return node_data;
}
else if(index==capacity()-1)
{
arrayListNode* temp(tail);
T node_data( tail->node_data );
tail = tail->prevListNode ;
if( tail )
tail->nextListNode = NULL;
else
head = NULL ;
delete temp;
s--;
return node_data;
}
arrayListNode* t;
arrayListNode* t1=head;
while(index>0)
{
t=t1;
t1=t1->nextListNode;
index--;
}
t->nextListNode=t1->nextListNode;
t1->nextListNode->prevListNode=t;
s--;
return t1->node_data;
}
int capacity()
{
return 100;
}
};
int main()
{
ArrayListLL<int> *alist;
alist->add( 11 );
alist->add( 100 );
system("pause");
}