简单的动态链表

来源:互联网 发布:甲骨文java培训骗局 编辑:程序博客网 时间:2024/06/04 19:20
题目:用动态链表输入三个学生的学号和成绩,并输出第一位学生的信息(c语言程序设计P313)
#include<stdio.h>#include<stdlib.h>#define LEN sizeof(struct student)struct student { long num; float score; struct student * next;//这里是指向结构体的指针,同时其也是结构体的一部分。对于新建的空间                                p->next是一个指针还没有赋值 };int n=3;// 要输入的学生数int i;struct student * creat(void){struct student *head,*p1;i=1;p1=(struct student *)malloc(LEN);head=(struct student *)malloc(LEN);scanf("%ld,%f",&p1->num,&p1->score);while(i<=n){p1->next=(struct student*)malloc(LEN);//建立连接,pl->next是结构体的一部分                pl=pl->next;//注意这一步和上一步的先后关系,pl只是变量               scanf("%ld,%f",&p1->num,&p1->score);i++;}p1->next=NULL; } int main(){struct student *pt;pt=creat();printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);} //从头到尾没有使用free(),很是不好,以后再加。