|
#include<iostream.h>
const int defaultSize=10;
template<class Type> class SeqList{
public:
SeqList(int MaxSize = defaultSize);
~SeqList(){ delete [] data;}
int Length() const {return last + 1;}
int Find(Type & x) const;
int IsIn(Type & x);
int Insert(Type & x, int i );
int Remove(Type & x);
int Next(Type & x);
int Prior(Type & x);
int IsEmpty(){ return last == -1;}
int IsFull(){ return last == MaxSize-1;}
Type Get(int i){ return i<0 || i>last? NULl :data[i];}
private:
Type * data;
int MaxSize;
int last;
};
template< class Type>
SeqList<Type>::SeqList(int sz){
if(sz > 0){
MaxSize=sz;
last = -1;
data = new Type[MaxSize];
}
}
template< class Type>
int SeqList<Type>::Find (Type & x)const{ //定位
int i=0;
while(i<= last && data[i] != x) i++; //顺序查找
if( i>last ) return -1;
else return i;
}
template< class Type>
int SeqList<Type>::IsIn (Type & x){ //判断X是否在表中
int i=0, found=0;
while(i<=last && !found)
if(data[i] !=x )i++;
else found = 1;
return found;
}
template<class Type>
int SeqList<Type>::Insert (Type & x, int i){ //插入x在表中第i个位置处
if(i<0 || i>last+1 || last == MasSize-1)return 0;
else {
last++;
for(int j=last;j>i;j--)data[j] = data[j-1];// 依次后移
data[i] = x;
return 1;
}
}
template<class Type>
int SeqList<Type>::Remove (Type & x){ //删除x
int i = Find(x); //在表中查找x
if(i>=0){
last--;
for(int j=i;j<=last;j++)data[j] = data[j+1];
return 1;
}
return 0; //x在表中不存在,不能删除
}
template<class Type>
int SeqList<Type>::Next (Type & x){
int i=Find(x);
if(i>=0 && i<last)return i+1; //x的后继存在
else return -1; //x不在表中或是x的后继不存在
}
template<class Type>
int SeqList<Type>::Prior (Type & x){ //寻找x 的前驱
int i=Find(x);
if(i>0 && i<=last)return i-1; //x的前驱位置
else return -1;
}
void main()
{
SeqList<int> myS;
cout<<myS.Insert(11, 0); //??????????????????????????????????????????????????
cout<<myS.Length ()<<endl;
}
高手给看一下,Compiling不通过啊 |
|