【文件属性】:
文件名称:约瑟夫环C++源代码
文件大小:1019B
文件格式:CPP
更新时间:2012-10-23 11:48:28
数据结构源代码
约瑟夫环的C++源代码
#include
using namespace std;
struct person
{
unsigned int number;
unsigned int key;
person *next;
};
class joseph_ring
{
private:
unsigned int n;
unsigned int m;
person *head;
public:
joseph_ring(){m=0;n=0;head=NULL;}
void create();
void show();
};
void joseph_ring::create()
{
cout<<"请输入人数:";
cin>>n;
cout<<"请输入m的初值:";
cin>>m;
person *p1,*p2;
for(int i=1;i<=n;i++)
{
p1=new person;
p1->number=i;
cout<<"请输入第 "<>p1->key;
if(i==1)
{
head=p1;
p2=p1;
}
else
{
p2->next=p1;
p2=p1;
}
}
p2->next=head;
}
void joseph_ring::show()
{
person *p1,*p2,*p;
p1=head;
for(int i=1;i<=n;i++)
{
int count=1;
while(count++next;
}
cout<number<<"\t";
m=p1->key;
p=p1;
p2->next=p1->next;
p1=p1->next;
delete p;
}
cout<>m;
}
int main()
{
joseph_ring j;
j.create();
j.show();
return 0;
}