线性表的顺序存储操作


顺序存储表的定义:
线性表的顺序存储结构指的是用一段地址连续的存储单元一次存储线性表中的数据元素。

顺序存储结构线性表的设计:
可以选择用 一维数组来数显顺序存储结构
--存储空间:T* array;
--当前长度:int m_length;
代码示例
#ifndef SEQLIST_H
#define SEQLIST_H
#include "List.h"

namespace CorlinLib {

template<typename T>
class SeqList : public List<T>
{
public:
    SeqList();
    /*@brief: 插入一个元素
     *@param: pos:元素插入的位置 val:插入元素的值
     *@ret:成功返回true失败返回false
    */
    virtual bool insert(int pos, const T& val);

    /*@brief: 删除一个元素
     *@param: pos:移除元素的位置
     *@ret:成功返回true失败返回false
    */
    virtual bool remove(int pos);
    virtual bool setValue(int pos, const T& val);
    virtual bool getValue(int pos, T& val)const;
    virtual int length() const;
    virtual void clear();

    //顺序表的数组下标访问方式
    T& operator [] (int pos);
    T operator [] (int pos) const;
    //顺序存储空间容量
    virtual int capacity() const = 0;

protected:
    T* m_array;
    int m_length;

};
}
#endif // SEQLIST_H
顺序存储表的元素获取操作:
--判断目标位置是否合法
--将目标位置作为数组下标获取元素
代码示例
bool getValue(int pos, T &val) const
{
    bool ret = false;
    if(pos >= 0 && pos < m_length)//判断目标位置合法性
    {
        val = m_array[pos];//获取元素
        ret = true;
    }
    return ret;
}

顺序存储表的元素插入操作:
--判断目标位置是否合法
--将目标位置之后的所有元素向后移动一个位置
--将新元素插入目标位置
--线性表长度加一
代码示例
bool insert(int pos, const T &val)
{
    bool ret = false;
    if(pos >= 0 && pos <= length && m_length + 1 < capacity())//判断目标位置是否合法
    {
        for(int i = m_length - 1; i >= pos; i--)//向后移动一位,寻找pos位置
        {
            m_array[i + 1] = m_array[i];
        }
        m_array[i] = val;
        m_length++;
        ret = true;
    }
    return ret;
}
顺序存储表的删除操作:
--判断目标位置是否合法
--将目标位置后的所有元素前移一个位置
--线性表长度减一
代码示例
bool remove(int pos)
{
    bool ret = false;
    if(pos >= 0 && pos < m_length)
    {
        for(int i = pos; i < length - 1; i++)//判断目标位置合法性
        {
            m_array[i] = m_array[i + 1];//将目标位置后的所有元素前移一个位置
        }
        m_length--;//线性表长度减一
        ret = true;
    }
    return ret;
}

其他相关接口实现
#ifndef SEQLIST_H
#define SEQLIST_H
#include "List.h"

namespace CorlinLib {

template<typename T>
class SeqList : public List<T>
{
public:
    SeqList();
    /*@brief: 插入一个元素
     *@param: pos:元素插入的位置 val:插入元素的值
     *@ret:成功返回true失败返回false
    */
    virtual bool insert(int pos, const T& val)
    {
        bool ret = false;
        if(pos >= 0 && pos <= m_length && m_length + 1 < capacity())//判断目标位置是否合法
        {
            for(int i = m_length - 1; i >= pos; i--)//向后移动一位,寻找pos位置
            {
                m_array[i + 1] = m_array[i];
            }
            m_array[i] = val;
            m_length++;
            ret = true;
        }
        return ret;
    }

    /*@brief: 删除一个元素
     *@param: pos:移除元素的位置
     *@ret:成功返回true失败返回false
    */
    virtual bool remove(int pos)
    {
        bool ret = false;
        if(pos >= 0 && pos < m_length)
        {
            for(int i = pos; i < m_length - 1; i++)
            {
                m_array[i] = m_array[i + 1];
            }
            m_length--;
            ret = true;
        }
        return ret;
    }

    virtual bool setValue(int pos, const T& val)
    {
        bool ret = false;
        if(pos >= 0 && pos < m_length)
        {
            m_array[pos] = val;
            ret = true;
        }
        return ret;
    }

    virtual bool getValue(int pos, T& val)const
    {
        bool ret = false;
        if(pos >= 0 && pos < m_length)
        {
            val = m_array[pos];
            ret = true;
        }
        return ret;
    }

    virtual int length() const
    {
        return m_length;
    }

    virtual void clear()
    {
        m_length = 0;
    }

    //顺序表的数组下标访问方式
    T& operator [] (int pos)
    {
        if(pos >= 0 && pos < m_length)
        {
            return m_array[pos];
        }
        else
        {
            //抛出异常
        }
    }

    T operator [] (int pos) const
    {
        return const_cast <SeqList<T>&>(*this);
    }

    virtual int capacity() const = 0;

protected:
    T* m_array;
    int m_length;

};

}

#endif // SEQLIST_H


SeqList是一个抽象类,关于顺序存储表的抽象,顺序表继承关系树:


猜你喜欢

转载自blog.csdn.net/naughfy/article/details/78565844