添加了一次加入多个元素和+的运算符重载
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#include <stdio.h>
using namespace std;
class ERROR {};
template <class T>
class SeqList
{
friend SeqList<T> operator+(const SeqList &a,const SeqList &b)
{ //友元函数进行加法的运算符重载
SeqList <T> c(a.MaxSize+b.MaxSize);
for (int i=0; i<a.CurrentLength; i++) c.data[i]=a.data[i];
for (int i=0;i<b.CurrentLength;i++) c.data[i+a.CurrentLength]=b.data[i];
c.CurrentLength=a.CurrentLength+b.CurrentLength;
return c;
}
private :
T*data;
int CurrentLength;
int MaxSize;
void DoubleSpace();
public:
SeqList (int InitSize =10);//构造函数
SeqList (const SeqList <T> &r);//拷贝构造函数
SeqList &operator= (const SeqList <T> &r);//赋值运算符重载
~SeqList (){delete [] data;}//析构函数
void clear(){CurrentLength= 0;}
bool IsEmpty(){return CurrentLength ==0;}
int length ()const {return CurrentLength ;}
void insert(int i ,const T &x);
void superadd (const int n);//一次性添加多个元素入表
void remove (int i);
int search(const T&x)const;
T visit(int i) const;
void traverse()const;
};
template <class T>
void SeqList <T> ::DoubleSpace()
{
T * tmp = data;
MaxSize *=2;
data = new T [MaxSize];
for (int i=0 ; i<CurrentLength ;++i) data[i]=tmp[i];
delete []tmp;
}
template <class T>
SeqList<T> ::SeqList (int InitSize )
{
if (InitSize <=0) throw ERROR();
data = new T[InitSize];
MaxSize = InitSize;
CurrentLength =0;
}
template <class T>
SeqList<T>::SeqList (const SeqList <T>&r)
{//拷贝构造函数的实现
CurrentLength=r.CurrentLength;
MaxSize=r.MaxSize;
data=new T [MaxSize];
for (int i=0;i<CurrentLength;++i) data[i]=r.data[i];//拷贝数据
}
template <class T>
void SeqList <T>::insert (int i,const T &x)
{
if (i<0||i>CurrentLength) throw ERROR();
if (CurrentLength ==MaxSize ) DoubleSpace();
for (int j=CurrentLength ; j<i;j-- )data [j]=data[j-1];
data[i]=x;
++CurrentLength;
}
template <class T>
void SeqList<T>::superadd(const int n) // superadd函数实现
{
if (n>(MaxSize-CurrentLength)) DoubleSpace();
T tmp;
cout<<"Please input the "<<n<<" datas you want to add to the end of the list in order"<<endl;
cout<<"Enter ENTER to finish your input."<<endl;
fflush(stdin);//清空输入缓存
int i=0;
while (cin>>tmp)
{
data[CurrentLength]= tmp;
CurrentLength++;++i;
if (i==n) break;
}
/*另一种实现方案
T*tmp;
tmp= new T[n];
cout<<"Please input the "<<n<<" datas you want to add to the end of the list in order"<<'\n';
fflush(stdin);
for(int i=0;i<n;i++) cin>>tmp[i];
for (int i=0; i<n ;i++) data[CurrentLength+i]=tmp[i];
CurrentLength+=n;
delete []tmp;
*/
}
template <class T>
void SeqList<T> ::remove(int i)
{
if (i<0||i>CurrentLength-1) throw ERROR();
for (int j=i ; j<CurrentLength-1 ;j++)data[j]=data[j+1];
--CurrentLength;
}
template <class T>
int SeqList <T>::search (const T &x) const
{
int i=0;
for (int i=0; i<CurrentLength&&data[i]!=x;++i);
if (i==CurrentLength) return -1;
else return i;
}
template <class T>
T SeqList <T> :: visit (int i)const
{
return data [i];
}
template <class T>
void SeqList<T>::traverse()const
{
for (int i=0;i<CurrentLength;i++)
cout<<data[i]<<' ';
cout<<endl;
}
template <class T> //赋值运算符重载的实现
SeqList<T> & SeqList<T>::operator=(const SeqList<T> &r)
{
if (this == &r) return *this; //防止自己复制自己
delete [ ] data; // 归还空间
CurrentLength=r.CurrentLength;
MaxSize=r.MaxSize;
data = new T [MaxSize];// 重新申请空间
for (int i=0; i <CurrentLength; ++i) //复制数组元素
data[i] = r.data[i];
return *this;
}
#endif // SEQLIST_H_INCLUDED