400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

数据结构(03)_顺序存储结构线性表-创新互联

基于前面实现的数据结构类模板基础,继续完成基于顺序存储结构的线性表的实现,继承关系图如下:
数据结构(03)_顺序存储结构线性表

江北网站建设公司成都创新互联,江北网站设计制作,有大型网站制作公司丰富经验。已为江北1000多家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的江北做网站的公司定做!

1.线性表简介

1.1.线性表的表现形式

template 
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.总结:

线性表是数据元素的有序并且有限的集合,其中的数据元素类型相同,在程序中表现为一个特殊的数据结构,可以使用C++中的抽象类来表示,用来描述排队关系的问题。

2.线性表的顺序存储结构

2.1.概念和设计思路

定义:
线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表中的数据元素。
数据结构(03)_顺序存储结构线性表
设计思路:
使用一维数组来实现存储结构:

// 存储空间:T* m_array; 当前长度:int m_length;
template 
class SeqList : public List
{
protected:
    T* m_array;
    int m_length;
};

3.SeqList的设计要点

template 
class SeqList : public List
{
protected:
    T* m_array;      // 顺序存储空间
    int m_length;    // 当前线性长度
public:

    bool insert(int index, const T& e)
    {
        bool ret = ( (index>=0) && (index<=m_length) ); // <= 因为可以插入的点,必然比当前元素多1

        if(ret && ( m_length < capacity() ))    // 当前至少有一个空间可插入
        {
            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=0) && (index=0) && (index=0) && (index&>(*this)[index];    // 去除const属性,然后调用非const版本实现
    }

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

4.StaticList和DynamicList

4.1.StaticList的设计要点:

类模板

template < typename T, int N >
class StaticList : public SeqList 
{
protected:
    T m_space[N];       // 顺序存储空间,N为模板参数
public:
    StaticList()        // 指定父类成员的具体值
    {
        this->m_array = m_space;
        this->m_length = 0;
    }
    int capacity() const
    {
        return N;
    }
};

4.3.DynamicList的设计要点:

类模板

template 
class DynamicList : public SeqList
{

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;im_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.顺序存储结构线性表分析

5.1.时间复杂度

顺序存储结构线性表的效率为O(n),主要受其插入和删除操作的影响(譬如插入操作时,要插入位置之后的数据要向后挪动) 。

5.2.问题

两个长度相同的顺序存储结构线性表,插入、删除操作的耗时是否相同?

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:数据结构(03)_顺序存储结构线性表-创新互联
标题URL:http://mzwzsj.com/article/dsicsp.html

其他资讯

让你的专属顾问为你服务