/*课程链表结构定义*/ typedef struct course { char cour[30]; int score; char teacher[10]; struct course *next; }C_COURSE; /*学生链表结构定义*/ typedef struct student { int num; char name[10]; char dep[20]; float ave; C_COURSE *link; struct student *next; }S_STUDENT;/*从键盘上输入信息并创建课程结构结点实例*//*分数为-1时,输入结束*//*返回所创建的接点地址,若输入结束返回NULL*/C_COURSE *creatcoursenode(){ C_COURSE *p=(C_COURSE*)malloc(sizeof(C_COURSE)); printf("course="); scanf("%s",p->cour); printf("teacher="); scanf("%s",p->teacher); printf("score="); scanf("%d",&p->score); if(p->score==-1){ free(p); return NULL; } else return p;}/*在课程链表中插入一个学生所学的所有课程*//*插入课程的成绩进行比较,并按照从高到低的顺序进行排列*//*h:课程链表的头接点的地址;course:要插入的接点*/void insercoursenode(C_COURSE **h,C_COURSE *course){ C_COURSE *p,*q; if(*h==NULL||course->score>=(*h)->score) { course->next=*h; *h=course; } else { q=*h; p=q->next; while(q!=NULL&&course->score<p->score) { q=p; p=p->next; } course->next=p; q->next=course; } } /*计算课程链表中课程的平均直*/ /*link:课程链表的头指针*/ /*返回链表中各门课的平均成绩*/ float aver(C_COURSE *link) { int n=0,total=0; while(link!=NULL) { total+=link->score; n++; link=link->next; } if(n) { return (float)(total/n); } else { return 0; }}/*从键盘输入创建学生接点*//*返回所创建的接点*/S_STUDENT * createstudentnode(){ C_COURSE *q; S_STUDENT *p=(S_STUDENT*)malloc(sizeof(S_STUDENT)); printf("number="); scanf("%d",&p->num); printf("name="); scanf("%s",p->name); printf("dep="); scanf("%s",p->dep); p->link=NULL; while((q=creatcoursenode())!=NULL){ insercoursenode(&(p->link),q); } p->ave=aver(p->link); return p;}/*学生链表中插入学生信息*/void insertstudentnode(S_STUDENT **h,S_STUDENT *stu){ S_STUDENT *p,*q; if(*h==NULL||stu->ave>=(*h)->ave){ stu->next=*h; *h=stu; } else{ q=*h; p=q->next; while(p!=NULL&&stu->ave<p->ave){ q=p; p=p->next; } stu->next=p; q->next=stu; }}

评论