线性表的顺序列表实现
教材:Data Structures and Algorithm Analysis in C++(Third Edition)
线性表:线性表是由element组成的有限且有序的序列,有序指的是每一个元素都有自己的位置,并非指按其值大小排序。而按照元素其值与元素位置的关系可以分为有序线性表(sorted list)和无序线性表(unsorted list)
位置:0~n-1
根据线性表的基本操作定义对象的抽象数据类型(ADT).定义抽象类List
template<typename E> class List{
private:
void operator =(const List&){};
List(const List&){};
public:
List(){}
virtual ~List(){}
virtual void clear() = 0;
virtual void insert(const E& item)= 0;
virtual void append(const E& item)= 0;
virtual E remove()= 0;
virtual void moveToStart() = 0;
virtual void moveToEnd() = 0;
virtual void prev() = 0;
virtual void next()= 0;
virtual int length() const = 0;
virtual int currPos()const = 0;
virtual void moveToPos(int Pos) = 0;
virtual const E& getValue() const = 0;
};
ADT中包含的关键设计是对当前位置(current position)的支持,如成员函数next和prev分别把当前位置移到下一个元素或者前一个元素。其含义是线性表的任何实现都支持当前位置这个概念。
定义顺序表实现类AList,该类必须实现List中所有的抽象函数
template<typename E> class AList : public List<E>{
private:
int maxSize;
int listSize;
int curr;
E* listArray;
public:
//设置顺序表默认大小为20;
AList(int size=20){
maxSize=size;
listSize = curr = 0;
listArray = new E[maxSize];
}
~AList(){ delete[] listArray; }
void clear(){
maxSize = curr = 0;
delete[] listArray;
listArray = new E[maxSize];
}
//插入
void insert(const E& it){
//Assert(listSize<maxSize,"capacity exceeded");
for (int i = listSize; i>curr; i--)
listArray[i] = listArray[i - 1];
listArray[curr] = it;
listSize++;
}
//表尾插入
void append(const E& it){
//Assert(listSize<maxSize, "capacity exceeded");
listArray[listSize] = it;
listSize++;
}
//删除表
E remove(){
//Assert((listSize<maxSize)||(listSize>0), "no element!");
E it = listArray[curr];
for (int i = curr; i < listSize - 1; i++)
listArray[i] = listArray[i + 1];
listSize--;
return it;
}
//重置当前位置
void moveToStart(){
curr = 0;
}
void moveToEnd(){
curr = listSize;
}
//向前移动
void prev(){
if (curr != 0) curr--;
}
//向后移动
void next(){
if (curr != maxSize) curr++;
}
//返回表长
int length() const{
return listSize;
}
//返回当期位置
int currPos() const{ return curr; }
//移动当前位置到pos
void moveToPos(int pos){
//Assert((pos >=0) || (pos <= listSize), "pos out of range!");
curr = pos;
}
//返回当前元素
const E& getValue() const{
//Assert((curr >= 0) || (curr < listSize), "no current element!");
return listArray[curr];
}
//倒置顺序表(后加的)
void inverse(){
for (int i = 0, j = listSize - 1; i < listSize / 2; i++, j--)
{
int temp = listArray[i];
listArray[i] = listArray[j];
listArray[j] = temp;
}
}
};
测试函数:
int main(){
AList<int> L1, L2;
int it;
L1.append(10);
L1.append(20);
L1.append(15);
int curpos = L1.currPos();
for (L1.moveToStart(); L1.currPos() < L1.length(); L1.next())
{
it = L1.getValue();
std::cout << it << std::endl;
}
std::cout << "L1的当前位置:" << curpos << std::endl;
L2.append(10);
L2.append(20);
L2.append(15);
L2.moveToStart();
L2.insert(39);
L2.next();
L2.insert(12);
curpos = L2.currPos();
for (L2.moveToStart(); L2.currPos() < L2.length(); L2.next())
{
it = L2.getValue();
std::cout << it << std::endl;
}
std::cout << "L2的当前栅栏:" << curpos << std::endl;
//4.3
//在构造函数中将maxSize修改为20;
AList<int> L3;
L3.append(2);
L3.append(15);
L3.append(5);
L3.append(9);
L3.moveToStart();
L3.next();
L3.insert(23);
for (L3.moveToStart(); L3.currPos() < L3.length(); L3.next())
{
it = L3.getValue();
std::cout << it << std::endl;
}
std::cout << "L3的当前位置:" << curpos << std::endl;
//4.6
L3.inverse();
std::cout << "L3逆序后为:" << std::endl;
for (L3.moveToStart(); L3.currPos() < L3.length(); L3.next())
{
it = L3.getValue();
std::cout << it << std::endl;
}
}
数据结构以前了解过一些,基本结构的概念和特点都懂一些,现在希望跟着课程进度一步一步重新实现一遍,一定会有不一样的认知和感受。
下一篇:线性表的链表实现