#include
#include
typedef struct node {
int data;
struct node *next;
} Lnode;
Lnode * create (int n)
{int i;
Lnode *h,*p,*r=(Lnode*)malloc(sizeof(Ln...ode));
r->data=n;
h=r;
for (i=n-1;i>0;i--)
{p=(Lnode*)malloc(sizeof(Lnode));
p->data=i;
p->next=h;
h=p;
}
r->next=h;
return h;
}
void jeseph(Lnode *p,int m)
{
Lnode *q; int j=0;
printf("outqueue order:\n");
while (p->next !=p)
{
j++;
if(j==m-1)
{
q=p->next;
p->next=q->next;//删除q结点
printf("%d ",q->data);//将q结点的数据输出
j=0;free(q);
}
p=p->next;//指针后移
}
printf("%d\n",p->data);
free(p);
}
int main ()
{
Lnode *h;
int m,n;
printf("Please input n,m="); //n=13 m=5
scanf("%d,%d",&n,&m);
h=create(n);
jeseph(h,m);
return 0;
}
运行:
Please input n,m=13,5
outqueue order:
5 10 2 8 1 9 4 13 12 3 7 11 6