简单的动态链表
2019-12-04 本文已影响0人
gtxe
每次输入学生的编号和成绩,如果学生的编号为0意味着结束,且不输出编号为0的学生的信息。
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
int main()
{
struct Student *head,*p1,*p2;
int n=0;
p1=p2=(struct Student *)malloc(LEN);
printf("input the num and score:\n");
printf("N01:\nnum:");scanf("%d",&p1->num);
printf("score:");scanf("%f",&p1->score);
head=NULL;
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
printf("N0%d:\nnum:",n+1);scanf("%d",&p1->num);
printf("score:");scanf("%f",&p1->score);
}
p2->next=NULL;
p1=head;
while(p1!=NULL)
{
printf("num:%d\tscore:%f\n",p1->num,p1->score);
p1=p1->next;
}
return 0;
}