|  | 
 
| 
#include "stdio.h"
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  #include "malloc.h"
 #include "stdlib.h"
 
 #define LEN sizeof(struct student)
 
 struct student
 {
 int num;
 float score;
 struct student *next;
 };
 struct student *build();
 struct student *insert(struct student *h);
 void print();
 void main()
 {
 struct student *head=NULL;
 head=build();
 print(head);
 head=insert(head);
 print(head);
 }
 struct student *build()
 {
 struct student *h,*newnode,*p;
 int n=0;
 p=newnode=h=NULL;
 while(1)
 {
 newnode=(struct student*)malloc(LEN);
 n++;
 if(n==1)
 h=newnode;
 else
 p->next=newnode;
 p=newnode;
 printf("输入学号:");
 scanf("%d",&newnode->num);
 printf("输入成绩:");
 scanf("%f",&newnode->score);
 if(newnode->num==0)
 break;
 }
 p->next=NULL;
 return h;
 }
 void print(struct student *h)
 {
 struct student *p;
 p=h;
 while(p->next)
 {
 printf("学号%d的成绩是:%f\n",p->num,p->score);
 p=p->next;
 }
 
 }
 struct student *insert(struct student *h)
 {
 struct student *p0 = NULL , *p1 , *p2;
 p0 = (struct student*) malloc (LEN);
 printf("输入要插入的学号:");
 scanf("%d",&p0->num);
 printf("输入成绩:");
 scanf("%f",&p0->score);
 p1 = p2 = h;
 if(h==NULL)
 {
 h=p0;
 p0->next=NULL;
 }
 else
 while(p0->num > p1->num && p1->next != NULL)
 {
 p2=p1;
 p1=p1->next;
 }
 if(p1->num > p0->num)
 {
 if(h==p1)
 {
 p0->next=h;
 h=p0;
 }
 else
 {
 p2->next=p0;
 p0->next=p1;
 }
 }
 else
 {
 p1->next=p0;
 p0->next=NULL;
 }
 
 
 
 return h;
 
 
 }
 | 
 |