[数据结构]向顺序表中插入元素时,若表满时则将表容量加倍,并将新元素插入表中
纠结很长时间了,我把出错的函数代码发上来bool SeqList<T>::Insert(int i,T x)
{
int j;
i=i-1;
//cout<<i<<"ttt1"<<endl;
if(i<-1||i>n-1)
{
cout<<"Out of Bounds"<<endl;
return false;
}
else if(n==maxLength)
{
//cout<<"Overflow"<<endl;
//return false;
T *array1=new T[maxLength];
for(j=0;j<n-1;j++)
array1[j]=elements[j];
SeqList<T>::~SeqList();
maxLength=2*maxLength;
SeqList<T>::SeqList(maxLength);//wait
//cout<<maxLength<<"here."<<endl;
//elements[maxLength+1]=x;
//cout<<elements[maxLength+5]<<"there"<<endl; for(j=0;j<n-1;j++) elements[j]=array1[j];
delete []array1;
}
//cout<<i<<"ttt2"<<endl;
for( j=n-1;j>i;j--) elements[j+1]=elements[j];
elements[i+1]=x;
cout<<sizeof(SeqList)<<"here";
//elements[0]=x;
//elements[1]=2;
//elements[3]=1;
//elements[4]=5;
n++; return true;
}