最新C++实现单链表(模版类).doc
《最新C++实现单链表(模版类).doc》由会员分享,可在线阅读,更多相关《最新C++实现单链表(模版类).doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品资料C+实现单链表(模版类).C+模版类实现单链表先上代码/*线性表抽象类的定义*/template 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的元素
2、在表中的位置 virtual dataType visit(int i)=0; /访问表中第i个元素的值 virtual void traverse()=0; /遍历线性表;/*单链表类的定义*/#include list.htemplate class linkList:public list /公有继承自list类 public: linkList(); /构造函数,创建对象时生成空链表 void empty(); /清空链表 int getLength(); /求表的长度 void insert(int i,const dataType& x); /在表中第i个位置插入值为x的元素 vo
3、id remove(int i); /删除表中第i个位置的元素 int search(const dataType& x); /查找并返回值为x的元素在表中的位置 dataType visit(int i); /访问表中第i个元素的值 void traverse(); /将表中的元素逐一输出 linkList(); /析构函数 private: /定义节点 struct node dataType data; node* next; node():next(NULL); node(dataType d):data(d),next(NULL); ; node* head; /链表头 node*
4、tail; /链表尾 int currentLength; /链表长度;/*单链表类的实现*/#include using namespace std;template linkList:linkList() head=new node; tail=new node; head-next=tail; currentLength=0; coutnCreate list success!n;template void linkList:empty() node* nowPos=head-next; while(nowPos!=tail) node* tmp=nowPos; nowPos=nowPo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 最新 C+ 实现 单链表 模版
限制150内