欢迎来到天天文库
浏览记录
ID:43490366
大小:72.07 KB
页数:7页
时间:2019-10-08
《大数阶乘 单链表》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
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
此文档下载收益归作者所有