#include <stdio h=""> #include <malloc h=""> #define NULL 0 #define LEN sizeof(struct student) struct student { long num; float score; struct student *next; }; int n; struct student *creat() //创建一个存放三个学生数据的链表 { struct student *head,*p1,*p2; n=0; p1=p2=(struct student*)malloc(LEN); head =NULL; printf("请输入学号和成绩\n"); scanf("%ld %f",&p1->num,&p1->score); while(p1->num!=0) { n+=1; if(n==1) head=p1; //else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); p2->next=p1; printf("请输入学号和成绩\n"); scanf("%ld %f",&p1->num,&p1->score); } p2->next=NULL; return(head); } struct student *add(struct student *head,struct student *stud) //插入结点 { struct student *p0,*p1,*p2; p1=head; p0=stud; if(head==NULL) { head=p0; p0->next=NULL; return(head); } else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(p1==head) head=p0; else p2->next=p0; p0->next=p1; } else { p1->next=p0; p0->next=NULL; } } n=n+1; return(head); } struct student *del(struct student *head,long num) //删除结点 { struct student *p1,*p2; if(head==NULL) { printf("list is null!\n"); return(head); } p1=head; while(p1->num!=num&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(p1->num==num) { if(p1==head) head=p1->next; else p2->next=p1->next; printf("delete:%ld\n",num); n=n-1; } else printf("%ld not been found!\n",num); return(head); } int print(struct student *head) //输出链表 { struct student *p; p=head; if(head!=NULL) { while(p!=NULL) { printf("%ld,%f\n",p->num,p->score); p=p->next; } } } int main() { struct student *p,*p0; p=creat(); print(p); p=del(p,10103); print(p); p0=(struct student*)malloc(LEN); p0->num=10102; p0->score=20; p=add(p,p0); print(p); printf("\n"); } </malloc></stdio>