




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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的元素在表中的位置 virtual dataType visit(int i)=0; /访问表中第i个元素的值 virtual void traverse()=0; /遍历线性表;/*双链表类的定义*/#include list.htemplate class dLinkList:public list /公有继承自list类 public: dLinkList(); /构造函数,创建对象时生成空链表 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(); /将表中的元素逐一输出 dLinkList(); /析构函数 private: /定义节点 struct node dataType data; /节点中保存的数据 node* prior; /节点的前驱 node* next; /节点的后继 node():prior(NULL),next(NULL); /构造函数 node(dataType d):data(d),prior(NULL),next(NULL); /构造函数 ; node* head; /链表头 node* tail; /链表尾 int currentLength; /链表长度;/*双链表类的实现*/#include using namespace std;template dLinkList:dLinkList() head=new node; tail=new node; head-prior=NULL,head-next=tail; tail-prior=head; currentLength=0; coutnCreate list success!n;template void dLinkList:empty() node* nowPos=head-next; while(nowPos!=tail) node* tmp=nowPos; nowPos=nowPos-next; delete tmp; head-next=tail; tail-prior=head; currentLength=0; coutnEmpty list success!n;template int dLinkList:getLength() return currentLength;template void dLinkList:insert(int i,const dataType& x) if(icurrentLength) coutnext=head-next,head-next-prior=add; add-prior=head,head-next=add; else /找到第i-1个节点 node* nowPos=head-next; while(-i) nowPos=nowPos-next; add-prior=nowPos,add-next=nowPos-next; nowPos-next-prior=add,nowPos-next=add; +currentLength; coutnInsert data success!n;template void dLinkList:remove(int i) if(i=currentLength) coutnext; while(i-) nowPos=nowPos-next; nowPos-prior-next=nowPos-next; nowPos-next-prior=nowPos-prior; delete nowPos; -currentLength; coutnDelete data success!n;template int dLinkList:search(const dataType& x) node* nowPos=head; int i; for(i=0;inext; if(nowPos-data=x) break; if(i=currentLength) return -1; return i;template dataType dLinkList:visit(int i) /i越界时抛出异常值0 if(i=currentLength) throw 0; /找到第i个节点 node* nowPos=head-next; while(i-) nowPos=nowPos-next; return nowPos-data;template void dLinkList:traverse() if(currentLength=0) coutnext; coutdata; nowPos=nowPos-next; while(nowPos!=tail) cout data; nowPos=nowPos-next; coutendl;template dLinkList:dLinkList() empty(); delete head; delete tail;注意1、这段代码是从线性表工程包里抽出来的,有必要可以去这里下载,工程包的内容如下:list.h-线性表抽象类的定义seqList.h-顺序表类的定义seqList.txt-顺序表类的实现linkList.h-单链表类的定义linkList.txt-单链表类的实现dLinkList.h-双链表类的定义dLinkList.txt-双链表类的实现test.h -测试类的定义test.txt-测试类的实现2、大家可以把这些代码作为参考,想要提高能力还是要自己写的;3、所有的类都是模版类,如果想把模版类的定义与实现分离要注意,模版类的实现
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 幼儿园地震安全培训试题及答案解析
- 2025湖南益阳市资阳区教育系统下属学校公益性岗位招聘10人考前自测高频考点模拟试题及答案详解参考
- 护理子宫破裂试题及答案
- 2025年初中英语考试题及答案
- 2025-2030工业级防爆指纹识别设备在油气领域的应用标准研究
- 2025-2030工业级3D打印设备应用拓展与投资潜力研究
- 2025-2030工业窑炉烟气碳捕集系统能耗控制与副产品商业化应用价值评估
- 2025-2030工业物联网安全防护体系构建报告
- 2025-2030工业气体分离膜材料性能突破与市场应用前景
- 手工皮具与箱包移动店创新创业项目商业计划书
- 九年级英语第1-3单元测试题(含答案)
- 锁骨骨折的护理查房
- 新12123交管学法减分考试题库及答案
- DB32T3728-2020工业炉窑大气污染物排放标准
- 重大风险管控方案及措施客运站
- 基于STM32智能书桌设计
- 《北京市基本概况》课件
- 设备维保中的环境保护与能源管理
- 混合型脑性瘫痪的护理课件
- 眼科专业视野培训教材
- 青蓝工程教师成长档案
评论
0/150
提交评论