#include "stdio.h"#include<malloc.h>#include<string.h>#include<ctype.h>#define LEN struct studentstruct student{ char name[22]; int Chinese; int Math; int English; struct student*next;};struct student*create(){ char ch; struct student *head; struct student*p1,*p2; int a=0; printf("是否输入新数据:\n"); while(toupper(ch=getchar())=='Y') { p1=(LEN*)malloc(sizeof(LEN)); printf("name:"); scanf("%s",&(*p1).name); printf("Chinese:"); scanf("%d",&(*p1).Chinese); printf("Math:"); scanf("%d",&(*p1).Math); printf("English:"); scanf("%d",&(*p1).English); getchar();//getchar吸收回车 printf("是否输入新数据:\n"); a++; if(a==1) head=p1; else (*p2).next=p1; p2=p1; p1->next=NULL; } return(head);}struct student * insert(struct student * head){ struct student *p0,*p1,*p2; p0=(struct student * )malloc(sizeof(LEN)); printf("\n输入所要插入节点的数据:"); scanf("%ld,%f",&num,&score); p1=head; if(head==NULL) { head=p0; p0->next=NULL; } else if(p1->num>p0->num) { //p0->next=head; head=p0;//可以删掉上一行吗,p0赋给head, //p0->next不会自动指向下一个节点吗.因为p0包含了next //不是吗 } else { while(p1!=NULL) { if(p1->num<p0->num) { p2=p1; p1=p1->next; } else { p0->next=p1; p2->next=p0; break; } } if(p1->next=NULL; { p0->next=NULL; p2->next=p0; } } return(head); }void print(struct student *head){ struct student *p; p=head; while(p!=NULL) { printf("\nname:%-8sChinese:%-5dMath:%-5 dEnglish:%-5d",p->name,p->Chinese,p->Math,p->English);//只是一个名 p=p->next;//测试随便输 }}void main( ){ //struct student *stu; //stu=create(); print(create());}
目前暂无任何回答
- 0 回答
- 0 关注
- 1513 浏览
添加回答
举报
0/150
提交
取消