#include <stdio.h>
#include <stdlib.h>
typedef int DataType;
typedef struct Seqlist
{
int n;
DataType *element;
}*PSeqList;
PSeqList createList_seq(int m)
{
int i;
PSeqList palist = (PSeqList)malloc(sizeof(struct Seqlist));
if(palist != NULL)
{
palist->element = (DataType *)malloc(sizeof(DataType) * m);
palist->n = m;
if(palist->element)
{
for(i = 0;i < palist->n;i++)
{
palist->element[i] = i + 1;
printf("%d,",palist->element[i]);
}
printf("/n");
return (palist);
}
else
free(palist);
}
printf("Out of space!/n");
return NULL;
}
void deleteP_seq(PSeqList palist,int p)
{
int i;
if(p < 0 || p > palist->n - 1 )
{
printf("Not exist!/n");
}
for(i = p;i < palist->n - 1;i++)
palist->element[i] = palist->element[i + 1];
palist->n = palist->n - 1;
}
void josephus_seq(PSeqList palist,int s,int m)
{
int s1,i;
for(i = palist->n;i>0;i--)
{
s1 = (s - 1 + m - 1) % i;//s-1---因为C语言数组下标从0开始,m-1---因为从自己数起,这样该删除的元素的下标就求出来了,i随着元素的减少而减少。
printf("Out element %d/n",palist->element[s1]);
deleteP_seq(palist,s1);
}
}
int main(void)
{
PSeqList jos_alist;
int n,s,m;
printf("/nplease input the value of n = ");
scanf("%d",&n);
printf("/nplease input the value (<=%d) of s = ",n);
scanf("%d",&s);
printf("/nplease input the value of m = ");
scanf("%d",&m);
jos_alist = createList_seq(n);
if(jos_alist != NULL)
{
josephus_seq(jos_alist,s,m);
free(jos_alist->element);
free(jos_alist);
}
return 0;
}