C实现约瑟夫环

#include "stdio.h"
#include "alloc.h"
#include "stdlib.h"
typedef struct student
{int num;
 int m;
 struct student *next;
}node,*link;
#define LEN sizeof(node)
link readdate(int *n)
{link head,p1,p2;
  int i;
 randomize();
  p2=NULL;
 printf("/ninput the number of n:");
 scanf("%d",n);
 head=NULL;
 for(i=1;i<=*n;i++)
 {
    p1=(link)malloc(LEN);
   if(i==1) head=p1;
   else p2->next=p1;
    p2=p1;
    p1->num=i;p1->m=random(5)+1;
 }
 free(p1);
 p2->next=head;
 return (head);
}
link inputdate(int *n)
{
  link p1,p2,head;
  int i;
  p2=NULL;
  printf("/ninput the number of n:");
  scanf("%d",n);
  for(i=1;i<=*n;i++)
  {
    p1=(link)malloc(LEN);
     if(i==1) head=p1;
     else p2->next=p1;
     p2=p1;
    p1->num=i;
    printf("/ninput the M of NO%d:",i);
    scanf("%d",&p1->m);
  }
 
   p2->next=head;
  return (head);
}    
void joseph(link l,int n)
{
  int i,j,m;
  link p1,p2,k;
  p1=k=l;m=6;
  for(i=0;i<n;i++)
     {
       for(j=1;j<m;j++)
            {    
               p2=p1;
               p1=p1->next;
            }
       printf("   %d    ",p1->num);
       m=p1->m;
       p2->next=p1->next;
       k=p1;
       free(p1);
       p1=k->next;
     
     }
      return ;
}
   
void print(link head,int n)
{link p;
 printf("/nNow, These %d records are:/n", n);
 p=head;
 if(p==NULL) return;
 do{printf("%5d  %8d/n", p->num, p->m);
    p=p->next;
   }while(p->next!=head);
   printf("%5d  %8d/n", p->num, p->m);
}
main()
{struct student *p;
 int n;
 int c;
printf("*******SELECT**********/n1.-------readdate/n2.-------inputdate/n0.-------exit");
while(1)
   {printf("/nselect the right number:");
    scanf("%d",&c);
    if(c==0) break;
    else {if(c==1)
      {p=readdate(&n);
       print(p,n);
       joseph(p,n);
      }
        if(c==2)
        { p=inputdate(&n);  
        print(p,n);
        joseph(p,n);
        } 
  } 
  }
}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值