运用循环单链表解决约瑟夫环问题

#include <iostream>  
using namespace std;  
struct node//构造结点
{  
    node(int a):data(a),next(NULL){}  //为结点初始化(分配空间)   
    int data;  
    node *next;  
};   
class josephus  
{  
public:  
    josephus(int x,int y,int z):a(x),b(y),c(z)//构造函数的初始化表列
    {  
create();  
        output();  
    }        
    void create();  //链表的初始化
    void output();  //输出数值  
private:  
    node *head;//循环链表的头节点  
    int a;     //链表节点个数  
    int b;     //第一个序号  
    int c;     //报数出局的数  
};  
void josephus::create()  
{  
    node *pre=NULL;  
    node *cur=NULL;  
    node *p=new node(1);  
    head=p;  
    cur=p;  
    for(int i=2;i<=a;i++)//为单链表初始化
    {  
        p=new node(i);  
        pre=cur;  
        cur=p;  
        pre->next=cur;  
    }  
    cur->next=head;                   //首尾相接,构造一个循环单链表    
    int n=c;  
    p=head;  
cout<<"输出运算前的结点(运算的起始位点):"<<endl;
    while(n--)  
    {  
        cout<<p->data<<",";  
        p=p->next;  
    }  
    cout << endl;  
}  
  
void josephus::output()  
{  
    node *pre=NULL;  
    node *cur=head;  
    b;  
    while(b--)                           //寻找第b个数(开始数值)  
    {  
        pre=cur;  
        cur=cur->next;  
    };
cout<<"依次输出被删除的数值:"<<endl;
    while(a--)                           //输出链表中所有的结点值
    {
for(int n=0;n<5;n++)             //控制输出函数的行数
{
int s=c-1;  
while(s--)                    //寻找间隔c的值
{
pre=cur;  
cur=cur->next;  
}  
node *p=cur;  
cout<<p->data<<"  ";  
cur=cur->next;                //删除此次输出的结点 
pre->next=cur;  
delete p;    
}
cout<<endl;
};
cout<<endl;
}  
  
int main()  
{
    josephus josephus(100,5,5);  //自定义初始化数值

    return 0;  

}  

运用循环单链表解决约瑟夫环问题