/*
線性表的幾個基本操作(ADT)
1:線性表的初始化: create()
2:線性表的撤銷: destroy()
3:判斷是否為空白: isempty()
4:求線性表的長度: length()
5:取表中元素: find(k,x)
6:按值尋找: search(x)
7:插入操作: insert(k,x)
8:刪除操作: delete(k,x)
9:線性表的輸出: output(k,x)
*/
//線性表
#ifndef AbstractList
#define AbstractList
template<class T>
class AbstractList
{
public:
virtual bool IsEmpty() const=0;
virtual int Length() const=0;
virtual bool Find(int K,T&x) const=0;
virtual int Search(const T& x) const=0;
virtual AbstractList<T>& Delete(int k,T& x)=0;
virtual AbstractList<T>& Insert(int k,const T& x)=0;
virtual void Output(ostream& out) const=0;
};
template<class T>
class LinearList::AbstractList<T>
{
public:
LinearList(int MaxListSize=10);
~LinearList(){ delete [] element;}
bool IsEmpty() const{ return length==0;}
int Length()const{return length;}
bool Find(int k,T & x)const;
int Search(const T& x)const;
LinearList<T>& Delete(int k ,T& x);
LinearLists<T>& Insert(int k,const T& x);
void Output(ostream& out) const;
private:
int length;
int MaxSize;
T *element;
};
template<class T>
LinearList<T>::LinearList(int MaxListSize)
{
MaxSize=MaxListSize;
element=new T[MaxSize];
length=0;
}
template<class T>
bool LinearList<T>::Find(int k,T & x)const
{
if(k<1 || k>length) return false;
x=element[k-1];
return true;
}
template<class T>
int LinearList<T>::Search(const T& x)const
{
for(int i=0;i<length;i++)
{
if(element[i]==x)
{
return ++i;
}
}
return 0;
}
template<class T>
LinearList<T>& LinearList<T>::Delete(int k,T& x)
{
if( k < 0 || K > length) throw OutOfBounds();
for( int i = k ; i < length ; i++ )
{
element[i-1]=element[i];
}
length--;
return *this;
}
template<class T>
LinearList<T>& LinearList<T>::Inseart(int k,const T& x)
{
if ( k < 0 || k > length+1) throw OutofBounds();
if(length == MaxSize) throw NoMem();
for(int i = length -1 ;i >= k;i--)
{
element[i+1]=element[i];
}
element[k]=x;
length++;
return *this;
}