josehus.h
#pragma once
struct person
{
int num;
person *next;
};
class josering
{
public:
josering(int x , int y );
~josering();
void getnum();
private:
person *head;
int n ;
int m ;
};
josehus.cpp
#include "josehus.h"
#include<iostream>
using namespace std;
josering::josering(int x,int y)
{
n=x;
m=y;
person *p,*end;
for(int i=1;i<=n;i++)
{
p=new person;
p->num=i;
if(i==1)
{
head=p;
end=p;
}
else
{
end->next=p;
end=p;
}
}
end->next=head;
}
josering::~josering()
{
person* p=head,*q;
while(p->next!=head)//p指向最后一个结点时结束循环
{
q=p;
p=p->next;
delete q;
}
delete p;//删除头结点
}
void josering::getnum()
{
person *p1,*p2,*p;
p1=head;
for(int i=1;i<=n-1;i++)
{
int count=1;
while(count++<m)
{
p2=p1;
p1=p1->next;
}
p=p1;
cout<<"第"<<i<<"轮:"<<p->num<<"出列"<<endl;
cout<<"----------------------------------------"<<endl;
p2->next=p1->next;
p1=p1->next;
delete p;
}
cout<<"最后出列的人的编号为:"<<p1->num<<endl;
}