溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

數(shù)據(jù)結(jié)構(gòu)(03)_順序存儲結(jié)構(gòu)線性表

發(fā)布時間:2020-07-13 04:42:35 來源:網(wǎng)絡(luò) 閱讀:451 作者:三九感冒靈 欄目:編程語言

基于前面實現(xiàn)的數(shù)據(jù)結(jié)構(gòu)類模板基礎(chǔ),繼續(xù)完成基于順序存儲結(jié)構(gòu)的線性表的實現(xiàn),繼承關(guān)系圖如下:
數(shù)據(jù)結(jié)構(gòu)(03)_順序存儲結(jié)構(gòu)線性表

1.線性表簡介

1.1.線性表的表現(xiàn)形式

  • 零個多多個數(shù)據(jù)元素組成的集合
  • 數(shù)據(jù)元素在位置上是有序排列的
  • 數(shù)據(jù)元素的個數(shù)是有限的
  • 數(shù)據(jù)元素的類型必須相同

    1.2.線性表的抽象定義、性質(zhì)

    線性表是具有相同類型的n(>=)個數(shù)據(jù)元素的有限序列,(a0, a1, a2... an-1),其中ai是表項,n是表長度。
    性質(zhì):

  • a0為線性表的第一個元素,只有一個后繼
  • an-1為線性表的最后一個元素,只有一個前驅(qū)
  • 其他數(shù)據(jù)項既有后繼,也有前驅(qū)
  • 支持逐項和順序存儲

    1.3.線性表的抽象實現(xiàn)

  • 插入、刪除數(shù)據(jù)元素
  • 獲取、設(shè)置目標(biāo)位置元素的值
  • 獲取線性表的長度
  • 清空線性表
template <typename T>
class List:public Object
{
protected:
    List(const List&);
    List& operator ==(const List&);

public:
    List(){}
    virtual bool insert(const T& e) = 0;
    virtual bool insert(int i,const T& e) = 0;
    virtual bool remove(int i) = 0;
    virtual bool set(int i,const T& e) = 0;
    virtual bool get(int i,T& e) const  = 0;
    virtual int length() const = 0;
    virtual void clear() = 0;
};

1.4.總結(jié):

線性表是數(shù)據(jù)元素的有序并且有限的集合,其中的數(shù)據(jù)元素類型相同,在程序中表現(xiàn)為一個特殊的數(shù)據(jù)結(jié)構(gòu),可以使用C++中的抽象類來表示,用來描述排隊關(guān)系的問題。

2.線性表的順序存儲結(jié)構(gòu)

2.1.概念和設(shè)計思路

定義:
線性表的順序存儲結(jié)構(gòu),指的是用一段地址連續(xù)的存儲單元依次存儲線性表中的數(shù)據(jù)元素。
數(shù)據(jù)結(jié)構(gòu)(03)_順序存儲結(jié)構(gòu)線性表
設(shè)計思路:
使用一維數(shù)組來實現(xiàn)存儲結(jié)構(gòu):

// 存儲空間:T* m_array; 當(dāng)前長度:int m_length;
template <typename T>
class SeqList : public List<T>
{
protected:
    T* m_array;
    int m_length;
};

3.SeqList的設(shè)計要點

  • 抽象類模板,存儲空間的大小和位置由子類完成;
  • 實現(xiàn)順序存儲結(jié)構(gòu)線性表的關(guān)鍵操作(增、刪、查、等);
  • 提供數(shù)組操作符重載,方面快速獲取元素;

    3.1SeqList實現(xiàn)

template <typename T>
class SeqList : public List<T>
{
protected:
    T* m_array;      // 順序存儲空間
    int m_length;    // 當(dāng)前線性長度
public:

    bool insert(int index, const T& e)
    {
        bool ret = ( (index>=0) && (index<=m_length) ); // <= 因為可以插入的點,必然比當(dāng)前元素多1

        if(ret && ( m_length < capacity() ))    // 當(dāng)前至少有一個空間可插入
        {
            for(int p=m_length-1; p>=index; p--)
            {
                m_array[p + 1] = m_array[p];
            }

            m_array[index] = e;
            m_length++;
        }
        return ret;
    }

    bool insert(const T& e)
    {
        return insert(m_length, e);
    }

    bool remove(int index)
    {
        bool ret = ( (index>=0) && (index<m_length) );  // 目標(biāo)位置合法 <m_length

        if(ret)
        {
            for(int p=index; p<m_length-1; p++)        // 注意思考此處的邊界條件
            {
                m_array[p] = m_array[p+1];
            }

             m_length--;
        }

        return ret;
    }

    bool set(int index, const T& e)
    {
        bool ret = ( (index>=0) && (index<m_length) );

        if(ret)
        {
            m_array[index] = e;
        }

        return ret;
    }

    bool get(int index, T& e) const
    {
        bool ret = ( (index>=0) && (index<m_length) );

        if(ret)
        {
            e = m_array[index];
        }

        return ret;
    }

    int length() const
    {
        return m_length;
    }

    void clear()
    {
        m_length = 0;
    }

    // 順序存儲表的數(shù)組訪問方式
    T& operator [] (int index)
    {
        if( (index>=0) && (index<m_length) )
        {
            return m_array[index];
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "index out of range...");
        }
    }

    T operator [] (int index) const
    {
        static_cast<SeqList<T>&>(*this)[index];    // 去除const屬性,然后調(diào)用非const版本實現(xiàn)
    }

    // 順序存儲表的的容量
    virtual int capacity() const = 0;
};

4.StaticList和DynamicList

4.1.StaticList的設(shè)計要點:

類模板

  • 使用原生數(shù)組做為順序存儲空間
  • 使用模板參數(shù)決定數(shù)組的大小
    template < typename T, int N >
    class StaticList : public SeqList <T>
    {
    protected:
    T m_space[];        // 順序存儲空間,N為模板參數(shù)
    public:
    StaticList();       // 指定父類成員的具體值
    int capacity() const;
    };

    4.2. StaticList實現(xiàn)

template < typename T, int N >
class StaticList : public SeqList <T>
{
protected:
    T m_space[N];       // 順序存儲空間,N為模板參數(shù)
public:
    StaticList()        // 指定父類成員的具體值
    {
        this->m_array = m_space;
        this->m_length = 0;
    }
    int capacity() const
    {
        return N;
    }
};

4.3.DynamicList的設(shè)計要點:

類模板

  • 申請連續(xù)堆空間做為順序存儲空間
  • 保證重置順序存儲空間的異常安全性
    函數(shù)異常安全的概念:
  • 不允許任何內(nèi)存泄露,不允許破壞數(shù)據(jù)
  • 函數(shù)異常安全的基本保證:
  • 如果有異常拋出,對象內(nèi)的任何成員任然能保持有效狀態(tài),沒有數(shù)據(jù)破話或者資源泄露。
    template < typename T>
    class DynamicList : public SeqList <T>
    {
    protected:
    int capacity;       // 順序存儲空間的大小
    public:
    DynamicList(int capacity);   // 申請空間
    int capacity(void) const         // 返回capacity的值 
    // 重置存儲空間的大小
    void reset(int capacity);
    ~DynamicList();             // 歸還空間
    };

    4.4. DynamicList實現(xiàn)

template <typename T>
class DynamicList : public SeqList<T>
{

protected:
    int m_capacity;
public:
    DynamicList(int capacity)
    {
        this->m_array = new T[capacity];
        if(this->m_array != NULL)
        {
            this->m_length = 0;
            this->m_capacity = capacity;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create DynamicList object ...");
        }
    }

    int capacity()const
    {
        return m_capacity;
    }

    void resize(int capacity)
    {
        if(capacity != m_capacity)
        {
            T* array = new T[capacity];
            if(array != NULL)
            {
                int length = (this->m_length < capacity ? this->m_length : capacity);
                for(int i=0;i<length;i++)
                {
                    array[i] = this->m_array[i];
                }

                T* temp = this->m_array;
                this->m_array = array;
                this->m_length = length;
                this->m_capacity = capacity;
                delete[] temp;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create DynamicList object ...");
            }
        }
    }

    ~DynamicList()
    {
        delete[] this->m_array;
    }
};

5.順序存儲結(jié)構(gòu)線性表分析

5.1.時間復(fù)雜度

順序存儲結(jié)構(gòu)線性表的效率為O(n),主要受其插入和刪除操作的影響(譬如插入操作時,要插入位置之后的數(shù)據(jù)要向后挪動) 。

5.2.問題

兩個長度相同的順序存儲結(jié)構(gòu)線性表,插入、刪除操作的耗時是否相同?

  • 不相同,對順序存儲結(jié)構(gòu)線性表,其插入、刪除操作的復(fù)雜度還取決于存儲的數(shù)據(jù)類型,譬如一個普通類型和一個字符串類型/類類型就完全不同(對于復(fù)雜數(shù)據(jù)類型,元素之間移動時必然耗時很多)。從這個角度考慮,線性表的效率存在隱患。
    花費多少

    5.3.禁用拷貝構(gòu)造和賦值操作。

    拷貝構(gòu)造和賦值操作會導(dǎo)致兩個指針指向同一個地址,導(dǎo)致內(nèi)存重復(fù)釋放。對于容器類型的類,可以考慮禁用拷貝構(gòu)造和賦值操作。
    原因: 1、對于生活中容器類的東西,我們無法對其進(jìn)行賦值(譬如生活中我們不可能將杯子中的水進(jìn)行復(fù)制,只能使用另一個杯子重新去獲取等量的水)。
    實現(xiàn):將拷貝構(gòu)造和賦值操作函數(shù)定義為proteced成員,在類的外部,不能使用。

    protected:
    List(const List&){}
    List& operator = (const List&){}

    5.4.注意事項

    線性表不能直接當(dāng)做數(shù)組來使用
    順序存儲結(jié)構(gòu)線性表提供了數(shù)組操作符的重載,可以直接像數(shù)組一樣,同過下標(biāo)直接獲取目標(biāo)位置的元素,在具體的使用上類似數(shù)組,但是本質(zhì)上不同,不能代替數(shù)組使用:

  • 必須先進(jìn)行插入操作,才能對其內(nèi)部的數(shù)據(jù)進(jìn)行操作。
  • 原生數(shù)組是自帶空間的,可以直接操作。
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI