若有不足望請(qǐng)指出
#include
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 10
typedef int Status;
typedef int ElemType;
typedef struct CircularList
{
ElemType data;
struct CircularList *next;
}CircularList,*LinkList;
Status Init_List(LinkList &L,int n) //建立無(wú)頭結(jié)點(diǎn)單循環(huán)鏈表
{
if(n<1) return ERROR;
L=(LinkList)malloc(sizeof(CircularList));
L->data=1;
if(n==1) {L->next=L; return OK;}
int i;
LinkList p,q;
for(i=2;i<=n;i++)
{
p=(LinkList)malloc(sizeof(CircularList)) ;
p->data=i;
if(i==2) {L->next=p; q=p;}
else {q->next=p;q=p;}
}
p->next=L;
return OK;
}
Status Delete_List(LinkList &L) //刪除L的下一節(jié)點(diǎn)
{
LinkList p=L->next;
if(p==L) return ERROR;
L->next=p->next;
free(p);
return OK; }
void JOSEPHUS(int n,int k,int m)
{//n為總?cè)藬?shù),k為第一個(gè)開(kāi)始報(bào)數(shù)的人,m為出列者喊到的數(shù)
LinkList L;
Init_List(L,n);
for(int i=1;i L=L->next;
int flag=1;
while(flag)
{
for(int i=1;i L=L->next;
flag=Delete_List(L);
L=L->next;
}
cout<<"The end:"<data< }
int main()
{
int n,k,m;
while(cin>>n>>k>>m)
JOSEPHUS(n,k,m);
return 0;}
1 1 1
The end:1
5 2 3
The end:5
8 5 3
The end:3
8 1 3
The end:7
#include
#include
#include
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 10
typedef int Status;
typedef int ElemType;
typedef struct CircularList
{
ElemType data;
struct CircularList *next;
}CircularList,*LinkList;
Status Init_List(LinkList &L,int n) //建立無(wú)頭結(jié)點(diǎn)單循環(huán)鏈表
{
if(n<1) return ERROR;
L=(LinkList)malloc(sizeof(CircularList));
L->data=1;
if(n==1) {L->next=L; return OK;}
int i;
LinkList p,q;
for(i=2;i<=n;i++)
{
p=(LinkList)malloc(sizeof(CircularList)) ;
p->data=i;
if(i==2) {L->next=p; q=p;}
else {q->next=p;q=p;}
}
p->next=L;
return OK;
}
Status Delete_List(LinkList &L) //刪除L的下一節(jié)點(diǎn)
{
LinkList p=L->next;
if(p==L) return ERROR;
L->next=p->next;
free(p);
return OK; }
void JOSEPHUS(int n,int k,int m)
{//n為總?cè)藬?shù),k為第一個(gè)開(kāi)始報(bào)數(shù)的人,m為出列者喊到的數(shù)
LinkList L;
Init_List(L,n);
for(int i=1;i
int flag=1;
while(flag)
{
for(int i=1;i
flag=Delete_List(L);
L=L->next;
}
cout<<"The end:"<
int main()
{
int n,k,m;
while(cin>>n>>k>>m)
JOSEPHUS(n,k,m);
return 0;}
1 1 1
The end:1
5 2 3
The end:5
8 5 3
The end:3
8 1 3
The end:7