include<iostream>
using namespace std;
const int MaxSize = 100;
template <class DataType>
class SeqList
{
public:
SeqList(){length=0;}
SeqList(DataType a[],int n);
~SeqList(){};
int Length(){return length;}//求长
DataType Get(int i);//按位查找
int Locate(DataType x);//按值查找
void Insert(int i,DataType x);//插入
DataType Delete(int i);//定位删除
void PrintList();//打印
void Reverse();//逆置
void Cancel(int i);//删除特定的数
void Sort();//(插入)排序
private:
DataType data[MaxSize];
int length;
};
template <class DataType>
SeqList<DataType>::SeqList(DataType a[],int n)
{
if(n>MaxSize) throw "wrong parameter";
for(int i=0;i<n;i++)
data[i]=a[i];
length=n;
}
template <class DataType>
DataType SeqList<DataType>::Get(int i)
{
if(i<1 && i>length) throw "wrong Location";
else return data[i-1];
}
template <class DataType>
int SeqList<DataType>::Locate(DataType x)
{
for(int i=0;i<length;i++)
if(data[i]==x) return i+1;
return 0;
}
template <class DataType>
void SeqList<DataType>::Insert(int i,DataType x)
{
if(length>=MaxSize) throw "Overflow";
if(i<1 || i>length+1) throw "Location";
for(int j=length;j>=i;j--)
data[j]=data[j-1];
data[i-1]=x;
length++;
}
template <class DataType>
DataType SeqList<DataType>::Delete(int i)
{
int x;
if(length==0) throw "Underflow";
if(i<1 || i>length) throw "Location";
x = data[i-1];
for(int j=i;j<length;j++)
data[j-1] = data[j];
length--;
return x;
}
template <class DataType>
void SeqList<DataType>::PrintList()
{
for(int i=0;i<length;i++)
cout<<data[i]<<endl;
}
template <class DataType>
void SeqList<DataType>::Reverse()
{
int j=length;
for(int i=0;i<j-1;i++)
{
int tmp=0;
tmp=data[i];
data[i]=data[j-1];
data[j-1]=tmp;
j=j-1;
}}
template <class DataType>
void SeqList<DataType>::Sort()
{
int i,j,temp;
for(i=1;i<length;i++)
{
temp=data[i];
for(j=i-1;j>=0&&temp<data[j];j--)
data[j+1]=data[j];
data[j+1]=temp;
}}
template <class DataType>
void SeqList<DataType>::Cancel(int x)
{
int i,a=0,k;
int j=length-1;
for(i=0;i<=j;i++)
{
int k=i;
if(data[k]==x)
{
a=1;
--length;
}
while(a==1&&k<=j)
{
data[k]=data[k+1];
k++;
}
a=0;
} }
int main()
{
SeqList<int> p;
SeqList<int> q;
p.Insert(1,5);
p.Insert(2,9);
p.Insert(3,3);
p.Insert(4,2);
p.Insert(5,7);
p.Sort();
//p.Reverse();
//p.Cancel(5);
p.PrintList();
return 0;
}
