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; }
相关文章推荐
- C++类模板 实现顺序表 从《数据结构》(C++版) 北京科海摘抄
- C++类模板实现顺序栈
- 单链表C++类模板实现
- 模板实现顺序表
- C++类模板实现双链表
- C++类模板 实现栈的链式存储结构 《数据结构》(北京科海) (部分摘抄)
- C++模板实现顺序表
- C++类模板方式实现单链表
- 队列(顺序存储)C++模板实现
- C++类模板实现循环队列
- [数据结构]队列之顺序队列的类模板实现
- CMAP原理及其在MFC中的实现(MAP模板没有顺序遍历的功能)
- 用模板实现顺序表和带头结点的双向循环链表
- 数据结构之动态数组 (C++类模板实现)
- c++类的模板成员函数申明和实现必须放在一起
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- CMAP原理及其在MFC中的实现(MAP模板没有顺序遍历的功能)
- C++模板实现动态顺序表(更深层次的深浅拷贝)与基于顺序表的简单栈的实现
- 你所不知道的事儿--C++类模板的声明和实现不能分离
- c++ 模板实现顺序表