约瑟夫环(c语言指针编写)

思路
**1.**使用两个指针(移动和删除)
**2.**创建单循环链表
**3.**查找并删除

# include 
# include 
typedef struct Node
{
 int data;
 int num;
 struct Node *next;
};
    struct Node *Create_List(int n)
{
 int c,i=1,j;
 struct Node *rear,*s,*CL;
 CL=(struct Node*)malloc(sizeof(struct Node));
 CL->next=CL;                          
 rear=CL;
 for(j=0;jdata=c;
  s->num=i;
  i++;
  rear->next=s;
  rear=s;
 }
 rear->next=CL->next ;
 return CL;
}
 struct Node*fun(struct Node *CL,int m)
 {
  int i;
  struct Node *p,*q;
  p=CL;
 for(i=1;i<=m;i++)
 {
  q=p;
  p=p->next;
 }
 printf("%d ",p->num);
 q->next=p->next;
 CL=p;
 return CL;
 }
 void main()
 {
  int n,m,a;
  struct Node*CL;
  printf("请输入m:");
  scanf("%d",&m);
  printf("请输入人数n:");
  scanf("%d",&n);
  printf("请输入每个人的密码:");
  CL=Create_List(n);
  printf("出列的顺序为:");
  CL=fun(CL,m);
  a=n-1;
  while(a!=1)
  {
   CL=fun(CL,CL->data);
   a--;
  }
  printf("\n最后剩下的人:%d",CL->next->num);
  printf("\n"); 
  return 0;
 }


你可能感兴趣的:(c语言)