顺序存储结构的抽象实现

 完成顺序存储结构线性表的抽象实现

在SeqList中的关键操作都已经实现了,但它还是一个抽象类,为什么呢?
顺序存储空间的指定并没有在SeqList中完成,由StaticList和DynamicList这两个子类中完成。

SeqList设计要点
——抽象类模板,存储空间的位置和大小由子类完成
——实现顺序存储结构线性表的关键操作(增,删,查,等)
——提供数组操作符,方便快速获取元素

template <typename T>
class SeqList: public List<T>
{
protected:
    T* m_array;   //顺序存储空间
    int m_length; //当前线性表长度
public:
    bool insert(int i, const T& e);
    bool remove(int i);
    bool set(int i, const T& e);
    bool get(int i, T& e) const;
    int length() const;
    void clear();
    //顺序存储线性表的数组访问方式 
    T& operator[](int i);
    T operator[] (int i) const;
    //顺序存储空间的容量
    virtual int capacity() const = 0; //代表顺序存储空间的最大容量,然而顺序存储空间的具体指定并不是在SeqList这个类中完成的。在其子类中实现
};

 具体实现:

#ifndef SEQLIST_H
#define SEQLIST_H

#include "List.h"
#include "Exception.h"

namespace DTLib
{
template <typename T>
class SeqList: public List<T>
{
protected:
    T* m_array;   //该指针指向具体的顺序存储空间,在子类中实现。
    int m_length; //当前线性表长度
public:
    bool insert(int i, const T& e)
    {
        bool ret =((0 <= i) && (i <= m_length));
        ret = ret && (m_length < capacity() );

        if(ret)
        {
            for(int p=m_length-1; p>=i; p--)
            {
                m_array[p+1] = m_array[p];
            }

            m_array[i] = e;
            m_length++;
        }

        return ret;
    }

    bool remove(int i)
    {
        bool ret =((0 <= i) && (i < m_length));

        if(ret)
        {
            for(int p=i; p<m_length-1; p++)
            {
                m_array[p] = m_array[p+1];
            }

            m_length--;
        }

        return ret;
    }

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

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

         return ret;
    }

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

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

        return ret;
    }
    int length() const
    {
        return m_length;
    }
    void clear()
    {
        m_length = 0;
    }
    //顺序存储线性表的数组访问方式
    T& operator[](int i)
    {
       if((0 <= i) && (i < m_length))
       {
           return m_array[i];
       }
       else
       {
           THROW_EXCEPTION(IndexOutOfBoundsException,"Parameter i is invalid...");
       }

    }
    T operator[] (int i) const
    {
#if 0
        if((0 <= i) && (i < m_length))
        {
            return m_array[i];
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException,"Parameter i is invalid...");
        }
#endif
        //考虑代码的复用性,那么就可以直接使用上面的非const版本的重载函数。
        //此时如果调用到了T operator[] (int i) const,说明当前对象是一个const对象,可以考虑将当前对象的const属性去掉,就可以使用T& operator[](int i)的实现了
        return const_cast<SeqList<T&> >(*this)[i];
    }
    //顺序存储空间的容量
    virtual int capacity() const = 0; //代表顺序存储空间的最大容量,然而顺序存储空间的具体指定并不是在SeqList这个类中完成的。在其子类中实现
};

}

#endif // SEQLIST_H
原文地址:https://www.cnblogs.com/-glb/p/12046474.html