欢迎来到天天文库
浏览记录
ID:39632252
大小:31.50 KB
页数:3页
时间:2019-07-07
《C++单链表实现约瑟夫环》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、#include#includestructMember//定义结构体{intnumber;intpassword;Member*next;};classJoseph{public:Member*frist;//头指针intsize;Joseph(void);~Joseph(void);intSize(void)const;//返回长度Member*Index(inti);//定位voidCreate(inti);//构造循环单链表intDelete(inti);//删除结点并返回number的值};Joseph::Jos
2、eph(){//frist=newMember;Member*p=newMember;frist=p;size=0;}Member*Joseph::Index(inti){if(i==0)returnfrist;Member*p=frist->next;intj=1;while(jnext;j++;}returnp;}intJoseph::Size(void)const{returnsize;}voidJoseph::Create(inti){for(intj=1;j<=i;j++){Member*p=Index(j-1);Member*q=new
3、Member;q->number=j;p->next=q;size++;};//Member*p=Index(i);//p->next=frist;}Joseph::~Joseph(void){Member*p,*q;p=frist;while(size!=0){q=p;p=p->next;deleteq;size--;}size=0;frist=NULL;}intJoseph::Delete(inti){Member*s,*p=Index(i-1);s=p->next;p->next=p->next->next;intx=s->number;cout<4、;inty=s->password;deletes;size--;returny;}voidmain(void){Josephjos;inti;cout<<"Pleaseinputnumberofpeople:"<>i;jos.Create(i);intfrist;//设初始值cout<<"Pleaseinputthefristnumber:"<>frist;for(intk=1;k<=i;k++)//用循环输入每个人的密码{cout<<"pleaseinputNo."<5、er*b=jos.Index(k);cin>>b->password;}cout<<"Thefinalis:"<0;b--){frist=jos.Delete(l);l=(frist+l-1)%b;inte=jos.Size();if(l==0)l=e;}jos.Delete(l);cout<
4、;inty=s->password;deletes;size--;returny;}voidmain(void){Josephjos;inti;cout<<"Pleaseinputnumberofpeople:"<>i;jos.Create(i);intfrist;//设初始值cout<<"Pleaseinputthefristnumber:"<>frist;for(intk=1;k<=i;k++)//用循环输入每个人的密码{cout<<"pleaseinputNo."<5、er*b=jos.Index(k);cin>>b->password;}cout<<"Thefinalis:"<0;b--){frist=jos.Delete(l);l=(frist+l-1)%b;inte=jos.Size();if(l==0)l=e;}jos.Delete(l);cout<
5、er*b=jos.Index(k);cin>>b->password;}cout<<"Thefinalis:"<0;b--){frist=jos.Delete(l);l=(frist+l-1)%b;inte=jos.Size();if(l==0)l=e;}jos.Delete(l);cout<
此文档下载收益归作者所有