您的位置:首页 > 编程语言 > C语言/C++

C++类模板实现顺序表

2011-11-09 22:12 351 查看
/********************************线性表抽象类的定义***************************/
template <class dataType>
class list{
public:
virtual void empty()=0;   //清空线性表
virtual int getLength()=0;   //求表的长度
virtual void insert(int i,const dataType& x)=0;   //在表中第i个位置插入值为x的元素
virtual void remove(int i)=0; //删除表中第i个位置的元素
virtual int search(const dataType& x)=0;  //查找并返回值为x的元素在表中的位置
virtual dataType visit(int i)=0;  //访问表中第i个元素的值
virtual void traverse()=0;    //遍历线性表
};

/*******************************顺序表类的定义********************************/
template <class dataType>
class seqList:public list<dataType>{    //公有继承自list类
public:
seqList(int initSize=10); //构造函数,默认设置表的初始容量为10
void empty(); //清除函数
int getLength(); //求表的长度
void insert(int i,const dataType& x);  //在表中第i个位置插入值为x的元素
void remove(int i);   //删除表中第i个位置的元素
int search(const dataType& x);    //查找并返回值为x的元素在表中的位置
dataType visit(int i);    //访问表中第i个元素的值
void traverse();  //遍历线性表
~seqList();
private:
dataType* data;   //表中第一个元素的地址
int maxLength;  //表的最大容量
int currentLength;    //表中元素的个数
void resize();    //将表的最大容量扩大一倍
};

/*********************************顺序表类的实现******************************/
template <class dataType>
seqList<dataType>::seqList(int initSize){
maxLength=initSize;
data=new dataType[maxLength];
currentLength=0;

cout<<"\nCreate list success!\n";
}

template <class dataType>
void seqList<dataType>::empty(){
currentLength=0;

cout<<"\nEmpty list success!\n";
}

template <class dataType>
int seqList<dataType>::getLength(){
return currentLength;
}

template <class dataType>
void seqList<dataType>::resize(){
dataType* tmp=data;
maxLength*=2;   //将表的容量扩大一倍
data=new dataType[maxLength];
for(int i=0;i<currentLength;++i){   //拷贝元素到新表
data[i]=tmp[i];
}
delete tmp;
}

template <class dataType>
void seqList<dataType>::insert(int i,const dataType& x){
if(i<0||i>currentLength){
cout<<"\nThe index is out of range!\n";
return;
}

if(currentLength==maxLength) resize();  //如果当前表满则扩容
for(int j=currentLength;j>i;--j){   //把i后面的元素后移一位
data[j]=data[j-1];
}
data[i]=x;
++currentLength;

cout<<"\nInsert data success!\n";
}

template <class dataType>
void seqList<dataType>::remove(int i){
if(i<0||i>=currentLength){
cout<<"\nThe index is out of range!\n";
return;
}

for(int j=i;j<currentLength-2;++j){   //将i后面的元素前移一位
data[j]=data[j+1];
}
--currentLength;

cout<<"\nDelete data sucess!\n";
}

template <class dataType>
int seqList<dataType>::search(const dataType& x){
int i;
for(i=0;i<currentLength;++i){
if(data[i]==x) break;
}
if(i==currentLength) return -1;
return i;
}

template <class dataType>
dataType seqList<dataType>::visit(int i){
//下标越界时抛出异常值0
if(i<0||i>=currentLength){
throw 0;
}

return data[i];
}

template <class dataType>
void seqList<dataType>::traverse(){
if(currentLength==0){
cout<<"\nThe list is empty!\n";
return;
}
for(int i=0;i<currentLength-1;++i){
cout<<data[i]<<" ";
}
cout<<data[currentLength-1]<<endl;
}

template <class dataType>
seqList<dataType>::~seqList(){
empty();
delete []data;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息