改好了可以运行,因为时间关系没有注释,你研究一下。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
long stuID;
char name[20];
char course[10];
float grade;
struct student *next;
};
typedef struct student stu;
stu *creatlist();
stu *insertlist(stu *p,stu *head);
void display(stu *head);
int main()
{
int num;
stu *head1,*head2,*head3;
stu *p;
char *course1="math",*course2="english",*course3="physics";
printf("pls enter the total number of students:\n");
scanf("%d",&num);
head1=creatlist();
head2=creatlist();
head3=creatlist();
while(num--)
{
p=(stu *)malloc(sizeof(stu));
printf("pls enter your student ID,then choose the course name:\n");
printf("name
student ID
course\n");
scanf("%s%ld%s",p->name,&p->stuID,p->course);
p->next=NULL;
if(!strcmp(p->course,course1))
head1=insertlist(p,head1);
if(!strcmp(p->course,course2))
head2=insertlist(p,head2);
if(!strcmp(p->course,course3))
head3=insertlist(p,head3);
}
if(head1->stuID!=0)
display(head1);
else free(head1);
if(head2->stuID!=0)
display(head2);
else free(head2);
if(head3->stuID!=0)
display(head3);
else free(head3);
return 0;
}
stu *creatlist()
{
stu *head;
head=(stu *)malloc(sizeof(stu));
if(head!=NULL)
{
head->next=NULL;
head->stuID=0;
}
else
{
printf("creat list head failed!\n");
exit(1);
}
return head;
}
stu *insertlist(stu *p,stu *head)
{
stu *pf,*pb,*t;
pb=head;
pf=head;
if(pb->next==NULL&&pb->stuID==0)
{
head=p;
head->next=NULL;
}
else
{
t=pf;
while(pb!=NULL&&p->stuID>=pb->stuID)
{
pf=t;
t=pb;
pb=pb->next;
}
if(t==head&&p->stuID<=head->stuID)
{
p->next=head;
head=p;
}
else
if(t->next==NULL&&p->stuID>=t->stuID)
{
pf->next=p;
p->next=NULL;
}
else
{
pf->next=p;
p->next=pb;
}
}
return head;
}
void display(stu *head)
{
while(head!=NULL)
{
printf("\n%s\t%ld
%s",head->name,head->stuID,head->course);
head=head->next;
}
}