请帅哥美女们进来帮小弟一个忙。谢了!
n个人围成一圈,从第一个人起,1,2,3的循环,拿到三号的退出,问最后剩下的是原来的几号?/* Note:Your choice is C IDE */
#include "stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
int j;
for(j=1;j<=n;j++)
{p1=p2=(struct student *)malloc(LEN);
&p1->num=j;
head=NULL;
while(p1->num!=0)
{
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=NULL;
return(head);
}
struct student *del(struct student *head)
{
struct student *p1,*p2;
int i;
if(head==NULL){printf("\nlist null!\n");return(head);}
p1=head;
for(i=0;;i++)
{
/*loop:*/ p2=p1;p1=p1->next;
if(i%3==0)
{/*if(p1==head)head=p1->next;*/
/*else*/ p2->next=p1->next;
n=n-1;
}
if(p1->next==NULL)
p1->next=head;
/* goto loop;*/
if(n==1)
return(head);
/* break;*/
}
}
void print(struct student *head)
{
struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{printf("%ld",p->num);
p=p->next;
}while(p!=NULL);
}
void main()
{
int n;
struct student *head;
printf("input n:\n");
scanf("%ld\n",&n);
head =creat();
print(head);
printf("\nnow begin to delete number three.");
head=del(head);
printf("\nnow begin to show the last number.");
print(head);
}帮忙修正一下,谢谢了。