这题的删除好像没作用啊,怎么改?
typedef struct student{
char name[10];
int score;
struct student *next;
}student;
#define NULL 0
struct student* creatform(struct student* L)/*建立单链表*/
{ int k,c;
char name[10];
struct student*s,*r;
r=L;
k=1;
printf("input name and score:\n");
while(k)
{
scanf("%s %d",&name,&c);
if(c!=0)
{
s=(struct student*)malloc(sizeof(struct student));
s->score=c;
strcpy(s->name,name);
r->next=s;
r=s;
}
else {k=0;r->next=NULL;}
}
return L;
}
void print(student *L,int x) /*打印出表中所有关键字值<=x的结点数据*/
{
student *a;
a=L->next;
while(a!=NULL)
{if(a->score<=x)
{printf("%s ",a->name); printf("%d\n",a->score);}
a=a->next;}
}
student * dele(student*L,int x)/*删除表中所有关键字值<=x的结点*/
{
student *r,*p1,*p2;
p1=L;
p2=p1->next;
while(p2!=NULL)
{
if((p2->score)<=x)
{r=p2;
p1->next=p2->next;
free(r);
p2=p1->next;}
else
{p1=p1->next;
p2=p2->next;}
}
return L;
}
void display(student *L) /*输出链表*/
{
student *a;
a=L->next;
while(a!=NULL)
{
printf("%s:",a->name);
printf("%d\n",a->score);
a=a->next;}
}
main()
{int x;
student *L,*a;
L=creatform( L);
a=L;
display(a);
a=L;
printf("input x:");
scanf("%d",&x);
print(a,x);
a=L;
printf("input x:");
scanf("%d",&x);
L=dele(a, x);
display(L);
getch();
}