魏はそう(剥き出しの歯)を構成する、配列の説明から始めるように見えることが判明しました
リニアテーブル抽象クラス
template<class T>
class linearList
{
public :
virtual ~linearList(){};
virtual bool empty()const=0;//当线性表为空时返回true
virtual int size()const=0;//返回线性表的元素个数
virtual T& get(int theIndex)const=0;//返回索引为theIndex的元素
virtual int indexOf(const T& theElement)const=0;//返回元素theElement第一次出现时的索引
virtual void erase(int theIndex)=0;//删除索引为theIndex的元素
virtual void insert(int theIndex,const T& theElement)=0;//把theElement插入线性表中索引为theIndex的位置上
virtual void output(ostream& out)const=0;//把线性表插入输出流
};
一次元アレイ(Oの時間複雑度(N))の長さを変化させます
template<class T>
void changeLength1D(T*& a,int oldLength,int newLength)
{
if(newLength<0)
throw illegalParameterValue("new length must be >= 0");
T* temp=new T[newLength];//新数组
int number=min(oldLength,newLength);//需要复制的元素个数
copy(a,a+number,temp);//将数组元素复制到新数组,需要用到头文件<algorithm>
delete []a;//释放旧数组的内存空间
a=temp;
}
ArrayListのアレイを有する線形テーブルが実装さ
template<class T>
class arrayList:public linearList<T>
{
public:
arrayList(int initialCapacity=10);//构造函数
arrayList(const arrayList<T>&);//复制构造函数
~arrayList(){delete [] element;}//析构函数
//ADT方法
bool empty()const{return listSize==0;}
int size()const{return listSize;}
T& get(int theIndex)const;
int indexOf(const T& theElement)const;
void erase(int theIndex);
void insert(int theIndex,const T& theElement);
void output(ostream &out)const;
//其他方法
int capacity()const {return arrayLength;}
protected:
void checkIndex(int theIndex)const;//若索引theIndex无效,则抛出异常
T *element;//存储线性表元素的一维数组
int arrayLength;//一维数组的容量
int listSize;//线性表的元素个数
};
ArrayListのコンストラクタ(O(1)の時間複雑さ)
template<class T>
arrayList<T>::arrayList(int initialCapacity)
{
if(initialCapacity<1)
{
ostringstream s;
s<<"Initial capacity="<<initialCapacity<<"Must be > 0";
throw illegalParameterValue(s.str());
}
arrayLength=initialCapacity;
element=new T[arrayLength];//动态申请长度为arrayLength的数组
listSize=0;
}
ArrayListのコピーコンストラクタ(O(n)との時間複雑)
template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList)
{
arrayLength=theList.arrayLength;
listSize=theList.listSize;
element=new T[arrayLength];
copy(theList.element,theList.element+listSize,element);
//将theList的element数组复制到element(本类,即this)中
}
実装(O(1)の時間複雑)checkIndex機能
//确定索引theIndex在0和listSize-1之间
template<class T>
void arrayList<T>::checkIndex(int theIndex)const
{
if(theIndex<0||theIndex>=listSize-1)
{
ostringstream s;
s<<"index = "<<theIndex<<"size = "<<listSize;
throw illegalIndex(s.str());
}
}
実装(O(1)の時間複雑さは)関数を取得します
//返回索引为theIndex的元素
template<class T>
T& arrayList<T>::get(int theIndex)const
{
checkIndex(theIndex);
return element[theIndex];
}
(時間複雑性O(MAX {LISTSIZE、1}))の機能を達成IndexOfメソッド
//寻找元素theElement第一次出现的位置
template<class T>
int arrayList<T>::indexOf(const T& theElement)const
{
int theIndex=(int)(find(element,element+listSize,theElement)-element);
//find函数用法:
//find(first,end,value) 寻找[first,end)区间里的第一个value出现的位置
//若找到value则返回指针或迭代器,若没找到则返回end
//确定元素theElement是否找到
if(theIndex==listSize)//没找到
return -1;
else
return theIndex;
}
達成消去機能(時間計算量O(LISTSIZE-theIndex))
//删除索引为theIndex的元素
template<class T>
void arrayList<T>::erase(int theIndex)
{
checkIndex(theIndex);
copy(element+theIndex+1,element+listSize,element+theIndex);
//theIndex之后的元素往前挪一个位置
element[--listSize].~T();//调用析构函数
}
達成関数挿入(時間複雑性O(LISTSIZE-theIndex))
//将元素theElement插入到索引为theIndex的位置
template<class T>
void arrayList<T>::insert(int theIndex,const T& theElement)
{
if(theIndex<0||theIndex>listSize)
{
ostringstream s;
s<<"index = "<<theIndex<<"size = "<<listSize;
throw illegalIndex(s.str());
}
if(listSize==arrayLength)//如果元素个数等于数组长度,说明数组已经满了
{
changeLength1D(element,arrayLength,2*arrayLength);
arrayLength*=2;
}
//把theIndex后的元素向后挪一个位置
copy(element+theIndex,element+listSize,element+theIndex+1);
element[theIndex]=theElement;//将元素theElement插入索引为theIndex的位置
listSize++;//不要忘了更新listSize!
}