主题:[讨论]哪位高手指点一下为什么这个程序插入不正确?
#define NULL 0
#include<malloc.h>
#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{int age;
float score;
struct student *next;
};
struct student *Create()
{
struct student *head,*p1,*p2;
int n=0;
p1=p2=(struct student *)malloc(LEN);
p1->score=-1;p1->age=-8888888;
head=NULL;
while(p1->age!=0)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%3d%2f",&p1->age,&p1->score);
head->score++;
}
p2->next=NULL;
return (head);
}
struct student *listdelete(struct student *L,int age)
{
struct student *q,*p;
printf("\nPlease input the student's age that you want to delete:");
scanf("%d",&age);
p=L;
while(p!=NULL&&p->age!=age)
{q=p;
p=p->next;}
if(p==NULL)
printf("There is no that student");
else {q->next=p->next;L->score--;}
return(L);
}
struct student *listinsert(struct student *L,int age)
{
struct student *p,*q,*s;
p=(struct student *)malloc(LEN);
printf("\nplease input the student's agge and score that you want to insert:");
scanf("%3d%2f",&p->age,&p->score);
p->next=NULL;
L->score++;
s=L;q=s;
if(s==NULL)
{ s=p;
return (L);}
else
while((s->next!=NULL)||(s->age<p->age))
{q=s;
s=s->next;}
if (s!=NULL)
{p->next=s;
q->next=p;}
else s->next=p;
return(L);
}
main()
{struct student *p,*head;
int age;
head=Create();
for(p=head;p!=NULL;p=p->next)
printf("%3d,%2f",p->age,p->score);
head=listdelete(head,age);
for(p=head;p!=NULL;p=p->next)
printf("%3d,%2f",p->age,p->score);
head=listinsert(head,age);
for(p=head;p!=NULL;p=p->next)
printf("%3d,%2f",p->age,p->score);
getchar();
getchar();
getchar();
getchar();
}
谢了!!
#include<malloc.h>
#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{int age;
float score;
struct student *next;
};
struct student *Create()
{
struct student *head,*p1,*p2;
int n=0;
p1=p2=(struct student *)malloc(LEN);
p1->score=-1;p1->age=-8888888;
head=NULL;
while(p1->age!=0)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%3d%2f",&p1->age,&p1->score);
head->score++;
}
p2->next=NULL;
return (head);
}
struct student *listdelete(struct student *L,int age)
{
struct student *q,*p;
printf("\nPlease input the student's age that you want to delete:");
scanf("%d",&age);
p=L;
while(p!=NULL&&p->age!=age)
{q=p;
p=p->next;}
if(p==NULL)
printf("There is no that student");
else {q->next=p->next;L->score--;}
return(L);
}
struct student *listinsert(struct student *L,int age)
{
struct student *p,*q,*s;
p=(struct student *)malloc(LEN);
printf("\nplease input the student's agge and score that you want to insert:");
scanf("%3d%2f",&p->age,&p->score);
p->next=NULL;
L->score++;
s=L;q=s;
if(s==NULL)
{ s=p;
return (L);}
else
while((s->next!=NULL)||(s->age<p->age))
{q=s;
s=s->next;}
if (s!=NULL)
{p->next=s;
q->next=p;}
else s->next=p;
return(L);
}
main()
{struct student *p,*head;
int age;
head=Create();
for(p=head;p!=NULL;p=p->next)
printf("%3d,%2f",p->age,p->score);
head=listdelete(head,age);
for(p=head;p!=NULL;p=p->next)
printf("%3d,%2f",p->age,p->score);
head=listinsert(head,age);
for(p=head;p!=NULL;p=p->next)
printf("%3d,%2f",p->age,p->score);
getchar();
getchar();
getchar();
getchar();
}
谢了!!