



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 linkList:public list /公有继承自list类 public: linkList(); /构造函数,创建对象时生成空链表 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(); /将表中的元素逐一输出 linkList(); /析构函数 private: /定义节点 struct node dataType data; node* next; node():next(NULL); node(dataType d):data(d),next(NULL); ; node* head; /链表头 node* 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=nowPos-next; delete tmp; head-next=tail; currentLength=0; coutnEmpty list success!n;template int linkList:getLength() return currentLength;template void linkList:insert(int i,const dataType& x) if(icurrentLength) coutnext=head-next; head-next=add; else /找到第i-1个节点 node* nowPos=head-next; while(-i) nowPos=nowPos-next; add-next=nowPos-next; nowPos-next=add; +currentLength; coutnInsert data success!n;template void linkList:remove(int i) if(i=currentLength) coutnext-next; delete head-next; head-next=tmp; else /找到第i-1个节点 node* nowPos=head-next; while(-i) nowPos=nowPos-next; node* tmp=nowPos-next-next; delete nowPos-next; nowPos-next=tmp; -currentLength; coutnDelete data success!n;template int linkList: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 linkList:visit(int i) /下标越界抛出异常值0 if(i=currentLength) throw 0; /找到第i个节点 node* nowPos=head-next; while(i-) nowPos=nowPos-next; return nowPos-data;template void linkList:traverse() if(currentLength=0) coutnext; coutdata; nowPos=nowPos-next; while(nowPos!=tail) cout data; nowPos=nowPos-next; coutendl;template linkList:linkList() 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、所有的类都是模版类,如果想把模版类的定义与实现分离要注意,模版类的实现不能放在.cpp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024秋八年级道德与法治上册 第二单元 公共利益 第四课 共同的需要 第2框 关涉全人类的公共利益说课稿 教科版
- 基于SDN的跟踪系统设计-洞察及研究
- Unit 5 Drink (教学设计)-2023-2024学年人教新起点版英语一年级下册
- 墨汁制造工协作考核试卷及答案
- 铌酸锂晶体制取工工艺考核试卷及答案
- 休闲农业服务员技能巩固考核试卷及答案
- 2024-2025学年七年级历史下册 第一单元 隋唐时期:繁荣与开放的时代 第1课 隋朝的统一与灭亡说课稿 新人教版
- 单轨吊司机操作考核试卷及答案
- 第17课 新民主主义革命的兴起教学设计-2023-2024学年中职历史中国历史高教版
- 小升初英语复习试卷及解析
- 【2025年】黄淮学院招聘事业编制硕士专职辅导员20名考试笔试试题(含答案)
- 2025年教师职称考试试题及答案
- 餐饮咨询顾问合同范本
- 2025年人教版小学五年级数学下册期末考试卷(附参考答案和解析)
- 2025年第九届“学宪法、讲宪法”知识竞赛题库及答案(中小学组)
- 部编人教版小学语文六年级上册【课内外阅读理解专项训练(完整)】含答案
- 2025年高考陕晋宁青卷地理试题解读及答案讲评(课件)
- 3.1生活在新型民主国家 教案 -2025-2026学年统编版道德与法治九年级上册
- 内镜中心课件
- 脑血管造影进修汇报总结
- 2025年(完整版)十八项核心制度培训考核试题(含答案)
评论
0/150
提交评论