C语言创建链表malloc的使用(C的创建对象)

来源:互联网 发布:帝国cms 模型 编辑:程序博客网 时间:2024/05/20 01:13
struct student *creat(){struct student *head;struct student *p1, *p2;p1 = p2 = (struct student *)malloc(LEN);//LEN是student结构的大小 这就是new对象printf("Please enter the num :");scanf("%d", &p1->num);printf("Please enter the score :");scanf("%f", &p1->score);head = NULL;n = 0;while(0 != p1->num){n++;if(1 == n){head = p1;}else{p2->next = p1;}p2 = p1;p1 =  (struct student *)malloc(LEN);printf("Please enter the num :");scanf("%d", &p1->num);printf("Please enter the score :");scanf("%f", &p1->score);}p2->next = NULL;return head;}
#include <malloc.h>#include <stdlib.h>
记得这些头文件
原创粉丝点击