初学者的问题
13个人围成一圈,从第一个人开始顺序报号1、2、3。凡报到3者退出圈子。找到最后留在圈子的人原来的序号。老谭书上的一个题,我自己写了一段程序,但是运行结果不对。用的TC 2.01版本,求达人解答!!
# include "malloc.h"
# define NULL 0
# define LEN sizeof(struct student)
struct student
{
int num;
struct student *next;
};
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=( struct student * )malloc(LEN);
scanf("%ld", &p1->num);
head=NULL;
while(p1->num!=14)
{
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *) malloc(LEN);
scanf("%ld",&p1->num);
}
p2->next=head;
return (head);
}
struct student *del(struct student *head )
{
struct student *p1,*p2;
p1=head;
n=0;
while(p1->next==NULL)
{
n=n+1;
p2=p1;
p1=p1->next;
if(n==3){p2->next=p1->next;p1=p1->next;n=0;}
}
return(p2);
}
int main()
{
printf("%ld", del(creat()));
return 0;
}