|
发表于 2017-5-24 17:08:05
|
显示全部楼层
- #include<stdio.h>
- #include<stdlib.h>
- #define LEN sizeof(struct student)
- struct student *create();
- void print(struct student *head);
- struct student
- {
- long num;
- float score;
- struct student *next;
- };
- int main()
- {
- struct student *stu;
- stu=create();
- print(stu);
- return 0;
- }
- struct student *create()
- {
- int n = 0;
- struct student *p1,*p2,*head;
- p2=p1=(struct student *)malloc(LEN);
- head=NULL;
- printf("请输入学号");
- scanf("%ld",&p1->num);
- printf("请输入成绩");
- scanf("%f",&p1->score);
- while(0!=p1->num)
- {
- n=n+1;
- if(n==1)
- head=p1;
- else p2->next=p1;
- p2=p1;
- p1=(struct student *)malloc(LEN);
- printf("请输入学号");
- scanf("%ld",&p1->num);
- printf("请输入成绩");
- scanf("%f",&p1->score);
- }
- p2->next=NULL;
- return head;
- }
- void print(struct student *head)
- {
- struct student *p;
- p=head;
- if(NULL!=head)
- {
- do{
- printf("学号是%ld的成绩是:%f\n",p->num,p->score);
- p=p->next;
- }while(p);
- }
- }
复制代码 |
|