#include<iostream>
using namespace std;
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty()const = 0;
virtual int size()const = 0;
virtual T& get(int theIndex)const = 0;
virtual int indexOf(const T& theElement)const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex, T& theElement) = 0;
virtual void Output(ostream out)const = 0;
};
template <class T>
class arrayList :public linearList<T>
{
public :
arrayList(int initialCapacity=10);
arrayList(const arrayList<T>& theList);
~arrayList() { delete[] element; }
bool empty()const { return listSize == 0; }
int size() const { return listSize; }
int capacity()const { return arrayLength; }
T& get(int theIndex)const;
int indexOf(const T& theElement)const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void Output(ostream& out)const;
protected:
void checkIndex(int theIndex)const;
T* element;
int arrayLength;
int listSize;
class iterator;
iterator begin() { return iterator(element); }
iterator end() { return iterator(element + listSize); }
};
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{
if (initialCapacity < 1)
{
ostringstream s;
s << "initialCapacity =" << initialCapacity << "Must be >0";
throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity;
element = new T[arrayLength];
listSize = 0;
}
template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
arrayLength = theList.arrayLength;
listSize = theList.listSze;
element = new T[arrayLength];
copy(theList.element, theList.elemnet + listSize, element);
}
template<class T>
void arrayList<T>::checkIndex(int theIndex)const
{
if (theIndex < 0 || theIndex >= listSize)
{
ostringstream s;
s << "index=" << theIndex << "size=" << listSize;
throw illegalindex(s.str());
}
}
template<class T>
T& arrayList<T>::get(int theIndex)const
{
checkIndex(theIndex);
return element[theIndex];
}
template<class T>
int arrayList<T>::indexOf(const T& theElement)const
{
int theIndex = (int)(find(element, element + listSize, theElement) - element);
if (theIndex == listSize)return -1;
else return theIndex;
}
template<class T>
void arrayList<T>::erase(int theIndex)
{
checkIndex(theIndex);
copy(element + theIndex + 1, element + listSize, element + theIndex);
element[--listSize].~T();
}
template<class T>
void arrayList<T> ::insert(int theIndex, const T& theElement)
{
if (theIndex<0 || theIndex>listSize)
{
ostringstream s;
s << "index=" << theIndex << "size=" << listSize;
throw illegalindex(s.str());
}
if (listSize == arrayLength)
{
changeLength1D(element, arrayLength, 2 * arrayLength);
arrayLength *= 2;
}
copy_backward(element + theIndex, element + listSize, element + theIndex + 1);
element[theIndex] = theElement;
listSize++;
}
template<class T>
void arrayList<T>::Output(ostream& out)const
{
copy(element, elemen + listSize, ostream_iterator<T>(cout, " "));
}
template<class T>
ostream& operator<<(ostream& out, const arrayList<T>& x)
{
x.Output(out);
return out;
}
int main()
{
int x[3] = { 0,1,2 };
for (int* y = x; y != x + 3; y++)
{
cout << *y << " ";
}
cout << endl;
}
template<class T>
class arrayList<T>::iterator {
public:
typedef bidirectional_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
iterator(T* thePosition = 0) {
position = thePosition;
}
T& operator*()const
{
return *position;
}
T* operator->()const
{
return &*position;
}
iterator& operator++()
{
++position;
return *this;
}
iterator operator++(int)
{
iterator old = *this;
++position;
return old;
}
iterator& operator--()
{
--position;
return *this;
}
iterator operator--(int)
{
iterator old = *this;
--position;
return old;
}
bool operator!=(const iterator right)const
{
return position != right.position;
}
protected:
T* position;
};
template<class T>
struct chainNode {
T element;
chainNode<T>* next;
chainNode() {}
chainNode(const T& element)
{
this->element = element;
}
chainNode(const T& element, const chainNode<T>* next)
{
this->element = element;
this->next = next;
}
};
template<class T>
class chain :public linearList<T>
{
public:
chain(int initialCapacity = 10);
chain(const chain<T>& c);
~chain();
bool empty()const { return listSize == 0; }
int size()const { return listSize; }
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
int indexOf(const T& theElement)const;
T& get(int theIndex)const;
void Output(ostream& out)const;
protected:
void checkIndex(int theIndex)const;
chainNode <T>* firstNode;
int listSize;
class iterator;
iterator begin() { return iterator(firstNode); }
iterator end() { return iterator(NULL); }
};
template<class T>
chain<T>::chain(int initialCapacity)
{
if (initialCapacity < 1)
{
ostringstream s;
s << "Initial capacity = " << initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
firstNode = NULL;
listSize = 0;
}
template<class T>
chain<T>::chain(const chain<T>& theList)
{
listSize = theList.listSize;
if (listSize == 0)
{
firstNode = NULL;
return;
}
chainNode<T>* sourceNode = theList.firstNode;
firstNode = new chainNode<T>(sourceNode->element);
sourceNode = sourceNode->next;
chainNode<T>* targetNode = firstNode;
while (sourceNode != NULL)
{
targetNode->next = new chainNode<T>(sourceNode->element);
targetNode = targetNode->next;
sourceNode = sourceNode->next;
}
targetNode->next = NULL;
}
template<class T>
chain<T>::~chain()
{
while (firstNode)
{
chainNode<T>* next = firstNode->next;
delete firstNode;
firstNode = next;
}
}
template<class T>
void chain<T>::checkIndex(int theIndex)const
{
if (theIndex < 0 || theIndex >= listSize)
{
ostringstream s;
s << "index=" << theIndex << "size=" << listSize;
throw illegalindex(s.str());
}
}
template<class T>
T& chain<T>::get(int theIndex)const
{
checkIndex(theIndex);
chainNode<T>* currentNode = firstNode;
for (int i = 0; i < theIndex; i++)
{
currentNOde = currentNode->next;
}
return currentNode->element;
}
template<class T>
int chain<T>::indexOf(const T& theElement)const
{
chainNode<T>* currentNode = firstNode;
int index = 0;
while (currentNode->next != NULL && currentNode->element != theElement)
{
currentNode = currentNode->next;
index++;
}
if (currentNode == NULL)return -1;
return index;
}
template<class T>
void chain<T>::erase(int theIndex)
{
chainNode<T>* deleteNode=firstNode;
checkIndex(theIndex);
if (theIndex == 0)
{
firstNode = firstNode->next;
}
else{
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
{
p = p->next;
}
deleteNode = p->next;
p->next = p->next->next;
}
listSize--;
delete deleteNode;
}
template<class T>
void chain<T>::insert(int theIndex, const T& theElement)
{
checkIndex(theIndex);
chainNode<T>* currentNode = firstNode;
if (theIndex == 0)
{
firstNode = new chainNode<T>(theElement, firstNode);
}
else {
for (int i = 0; i < theIndex - 1; i++)
{
currentNode = currentNode->next;
}
currentNode->next = new chainNode<T>(theElement, currentNode->next);
}
listSize++;
}
template<class T>
void chain<T>::Output(ostream& out)const
{
for (chainNode<T>* currentNode = firstNode; currentNode != NULL; current = current->next)
{
out << currentNode->element << " ";
}
}
template <class T>
ostream& operator<<(ostream& out, const chain<T>& x)
{
x.Output(out); return out;
}
template <class T>
class chain<T>::iterator
{
public:
iterator(chainNode<T>* theNode = NULL)
{
node = theNode;
}
T& operator*()const { return node->element; }
T* operator->()const { return &node->element; }
iterator& operator++()
{
node = node->next;
return *this;
}
iterator operator++(int)
{
iterator old = *this;
node = node->next;
return old;
}
bool operator!=(const iterator right)const
{
return node != right.node;
}
bool operator==(const iterator right)const
{
return node == right.node;
}
protected:
chainNode<T>* node;
};
template<class T>
class extendedLinearList :linearList<T>
{
public:
virtual ~extendedLinearList() {};
virtual void clear()const = 0;
virtual push_back(const T& theElement) = 0;
};