C++实现单链表(模版类).doc_第1页
C++实现单链表(模版类).doc_第2页
C++实现单链表(模版类).doc_第3页
C++实现单链表(模版类).doc_第4页
免费预览已结束,剩余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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论