33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
struct student *next;
};
struct student *creat(void)
{
struct student *head ,*p1,*p2;
int n,i;
p1=p2=(struct student *)malloc(LEN);
i=1;n=0;
p1->num=i;
head=NULL;
while(p1->num <=50)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
p1->num=++i;
}
p2->next=head; //
return(head);
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
int i,n=num;
do
{
if(n==num)p1=head;
else p1=p1->next;
for(i=0;i <=1;i++)//跳2个
{
p2=p1;
p1=p1->next;
}
p2->next=p1->next;
n--;
}
while(n>1);
return(p1);
}
int main()
{
int i=0;
struct student *p, *head;
p=creat();
head=p;
if(p!=NULL)
do
{ if((++i)%10==0)
printf("\n");
printf("%d ",p->num);
p=p->next;
}
while(p!=head); //条件错误
printf("\n最后剩下的数字是:%d",del(head,50)->num);
getchar();
}