400 028 6601

建站动态

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

数据结构(03)_顺序存储结构线性表

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

涵江网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。成都创新互联2013年开创至今到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

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.问题

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


新闻标题:数据结构(03)_顺序存储结构线性表
浏览地址:http://www.bluegullmedia.com/article/jcgchp.html

其他资讯

让你的专属顾问为你服务

0.0507s