数据结构--线性表之StaticList类

来源:互联网 发布:js特效比较好的网站 编辑:程序博客网 时间:2024/06/16 22:33

StaticList设计要点:

首先依旧还是使用类模板实现

继承自SeqList

使用原生数组作为顺序存储空间

使用数值模板参数决定数组大小

实现如下:

    template <typename T,int N>    class StaticList : public SeqList<T>    {    protected:        T m_space[N];//顺序存储空间,N为模板参数    public:        StaticList()//指定父类成员的具体值        {            this->m_array = m_space;            this->m_length = 0;        }        int capacity() const        {            return N;        }    };
在构造函数里实现m_array和m_length的赋值,m_array指向原生数组所在的空间,获取最大容量的实现即返回数值模板参数N。

测试代码:

    StaticList<int,5> list;    for(int i = 0; i < list.capacity(); i++)    {        list.insert(0,i);    }    for(int i = 0; i < list.length(); i++)    {        cout << list[i] << endl;    }    cout << endl;    list[0] *= list[0];    cout << list[0] << endl;
输出结果:

4
3
2
1
0

16
StaticList类的实现还是十分简单的,下一篇就开始实现比较有难度的DynamicList类。