大数阶乘 单链表

大数阶乘 单链表

ID:43490366

大小:72.07 KB

页数:7页

时间:2019-10-08

大数阶乘 单链表_第1页
大数阶乘 单链表_第2页
大数阶乘 单链表_第3页
大数阶乘 单链表_第4页
大数阶乘 单链表_第5页
资源描述:

《大数阶乘 单链表》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、//大数阶乘_单链表.cpp:Definestheentrypointfortheconsoleapplication.////filechain.h#ifndefChain_#defineChain_#include#include#include"cnode.h"#include"xcept.h"templateclassChainIterator;templateclassChain{friendChainIterator;public:Chai

2、n(){first=0;}//构造函数~Chain();//析构函数boolIsEmpty()const{returnfirst==0;}//判断链表是否为空intLength()const;//求链表的长度boolFind(intk,T&x)const;//查找第k个元素intSearch(constT&x)const;//查找元素xChain&Delete(intk,T&x);//删除第k个元素Chain&Insert(intk,constT&x);//在第k个元素之后插入xvoidOutput(ostream&out

3、)const;//单链表的输出Chain&Fac(longn);//求大数阶乘private:ChainNode*first;//指向第一个节点};templateChain::~Chain(){//删除所有的节点ChainNode*next;while(first){next=first->link;deletefirst;first=next;}}templateintChain::Length()const{//返回链表的长度ChainNode*current=

4、first;intlen=0;while(current){len++;current=current->link;}returnlen;}templateboolChain::Find(intk,T&x)const{//查找第k个元素,并赋值给xif(k<1)returnfalse;ChainNode*current=first;intindex=1;while(indexlink;index++;}if(current){x=current->

5、data;returntrue;}returnfalse;}templateintChain::Search(constT&x)const{//查找元素x,返回该元素的下标ChainNode*current=first;intindex=1;while(current&¤t->data!=x){current=current->link;index++;}if(current)returnindex;return0;}templateChain&Chain::Dele

6、te(intk,T&x){//删除第k个元素,并赋值给x,返回改变后的链表if(k<1

7、

8、!first)throwOutOfBounds();ChainNode*p=first;if(k==1)first=first->link;else{ChainNode*q=first;for(intindex=1;indexlink;if(!q

9、

10、!q->link)throwOutOfBounds();p=q->link;q->link=p->link;}x=p->data;deletep;

11、return*this;}templateChain&Chain::Insert(intk,constT&x){//在第k个元素之后插入x,返回改编后的链表if(k<0)throwOutOfBounds();ChainNode*p=first;for(intindex=1;indexlink;if(k>0&&!p)throwOutOfBounds();ChainNode*y=newChainNode;y->data=x;if(k){y->link=p-

12、>link;p->link=y;}else{y->link=first;first=y;}return*this;}templatevoidChain::Output(ostream&out)con

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。