# include<stdio.h>
# include<malloc.h>
# define size sizeof(struct Student)
struct Student
{
char name[12];
float score;
struct Student *next;
};
int main()
{
struct Student stu1,stu2,*head,*p;
head=p=&stu1;
head=(struct Student *)malloc(size);
printf("请输入学生姓名:");
scanf("%s",head->name);
printf("请输入学生成绩:");
scanf("%f",&head->score);
p=(struct Student *)malloc(size);//开辟另一个空间
printf("请输入学生姓名:");
scanf("%s",p->name);
printf("请输入学生成绩:");
scanf("%f",&p->score);
head->next=p;//将第二个开辟空间的首地址赋给head->next
p->next=NULL; //NULL在studio.h中定义为0;
p=head;
printf("学生一:\n");
printf("学生姓名:%s\n",p->name);
printf("学生成绩:%.2f\n",p->score);
p=p->next;
printf("学生二:\n");
printf("学生姓名:%s\n",p->name);
printf("学生成绩:%.2f\n",p->score);
return 0;
}
动态链表 malloc
最新推荐文章于 2023-12-21 23:48:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)