|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<malloc.h>
- #include<stdlib.h>
- #define NULL ((void *)0)
- #define LEN sizeof(struct student)
- struct student
- {
- long num;
- float score;
- struct student *next;
- };
- int n;
- struct student *creat(void)
- {
- struct student *head;
- struct student *p1,*p2;
- n=0;
- p1=p2=(struct student*)malloc(LEN); //开辟新的空间并把空间的首地址给p1p2
- printf("input a num: ");
- scanf("%ld",&p1->num);
- printf("input a score: ");
- scanf("%5.1f",&p1->score); //如果这里写scanf("%f",&p1->score);输出就没有问题.
- head=NULL; //头指针为空
- while(p1->num)
- {
- n=n+1;
- if(n==1)
- head=p1;
- else
- {
- p2->next=p1;
- }
- p2=p1;
- p1=(struct student*)malloc(LEN);
- printf("\ninput a num: "); //开辟新空间
- scanf("%ld", &p1->num);
- printf("input a score: ");
- scanf("%5.1f", &p1->score); //如果这里写scanf("%f",&p1->score);输出就没有问题.
- }
- p2->next=NULL;
- return(head);
- }
- void print(struct student *head)
- {
- struct student *p;
- printf("\nNow,These %d records are:\n\n",n);
- p=head;
- if(head!=NULL)
- do
- {
- printf("this num %ld is score%5.1f:\n",p->num,p->score);
- p=p->next;
- }
- while(p!=NULL);
- }
- main()
- {
- struct student *head;
- head=creat();
- print(head);
- system("pause");
- }
复制代码
|
|