數據結構學習——線性表的數組描述

阿偉發現好像要從數組描述開始,所以補上(呲牙)

線性表的抽象類

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)中
}

checkIndex函數的實現(時間複雜度O(1))

//確定索引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());
    }
}

get函數的實現(時間複雜度O(1))

//返回索引爲theIndex的元素
template<class T>
T& arrayList<T>::get(int theIndex)const
{
    checkIndex(theIndex);
    return element[theIndex];
}

indexOf函數的實現(時間複雜度O(max{listSize,1}))

//尋找元素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;
}

erase函數的實現(時間複雜度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();//調用析構函數
}

insert函數的實現(時間複雜度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!
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章