#include "stdio.h"

#define N 9
#define OVERFLOW 0
#define OK 1
int KeyW[N]={4,7,5,9,3,2,6,1,8};




typedef struct LNODE{
   int keyword;
   struct LNODE *next;
}LNODE,*Linklist;


void Joseph(Linklist p,int m,int x){
    Linklist  q;
    int i;
    if(x==0)return;
    q=p;
    m%=x;
    if(m==0)m=x;
    for(i=1;i<=m;i++){
        p=q;
        q=p->next;
     }
    p->next=q->next;
    i=q->keyword;
    printf("%d",q->keyword);
    free(q);
    Joseph(p,i,x-1);
}


int main()
{
    int i,m;
    Linklist Lhead, p,q;
    Lhead=(Linklist)malloc(sizeof(LNODE));
    if(!Lhead) return OVERFLOW;
    Lhead->keyword=KeyW[0];
    Lhead->next=NULL;
    p=Lhead;
    for(i=1;i<9;i++){
      if(!(q=(Linklist)malloc(sizeof(LNODE)))) return OVERFLOW;
      q->keyword=KeyW[i];
      p->next=q;
      p=p->next;  /* q=p;!!!*/
      q=NULL;
    }
    p->next=Lhead;
    printf("input m:\n");
    scanf("%d",&m);
    printf("The output alignment is:\n");
    Joseph(p,m,N);
      getch();
    return OK;

}