simpleCY
线性表的顺序存储结构

顺序存储的定义:

线性表的连续存储结构,指的是用一段地址连续的存储单元依次存储线性表中的数据元素。

由地址连续的存储单元可以想到C++中的数组。接下来使用数组实现线性表的顺序存储。

 

SeqList顺序存储结构线性表设计要点:

抽象类模板,存储空间的位置和大小由子类完成。

实现顺序存储结构线性表的关键操作(增删查等)

提供数组操作符,方便快速获取元素

 

代码如下SeqList.h:

#pragma once

#include"List.h"

#include<iostream>

namespace myLib{

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 = (i >= 0) && (i <= m_length);

ret = ret && ((m_length+1)<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 removve(int i)

{

bool ret = (i >= 0) && (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 = (i >= 0) && (i <= m_length);

if (ret)

{

m_array[i] = e;

}

return ret;

}

bool get(int i, T&e)const

{

bool ret = (i >= 0) && (i <= m_length);

if (ret)

{

r = m_array[i];

}

return ret;

}

int length()const

{

return m_length;

}

void clear()

{

m_length = 0;

}

//访问方式

T& operator[] (int i)

{

if ((i >= 0) && (i <= m_length))

{

return m_array[i];

}

else{

std::cout << "out of inidex";

}

}

T operator[](int i)const

{

return const_cast<SeqList<T>&>(*this)[i];

}

//顺序存储空间的容量

virtual int capacity() const = 0;

};

}

分类:

技术点:

相关文章: