#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);
}
}
}
}