码迷,mamicode.com
首页 > 编程语言 > 详细

C语言链表创建

时间:2018-05-10 19:29:57      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:动态链表   sizeof   int   ext   数据   def   scanf   include   \n   

#include "malloc.h"
#include "stdio.h"
#define LEN sizeof(struct student)

typedef struct student
{
int num;
int age;
float score;
struct student *next;
}stu;
int n;
// 创建动态链表函数
stu *creat(void)
{
//定义结构体类型的指针
stu *head,*p1,*p2;
n=0;
p1=p2=(stu *)malloc(LEN);//开辟一个内存空间
// 输入结构体类型的数据
scanf("%d,%d,%f",&p1->num,&p1->age,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if (n==1)head=p1;
else
// 单向链表
p2->next=p1;
p2=p1;
p1=(stu *)malloc(LEN);
scanf("%d,%d,%f",&p1->num,&p1->age,&p1->score);
}
p2->next=NULL;
return(head);
}
main()
{
stu *p,*head;
head=creat();
if (head!=NULL)
{
do
{
printf("%d,%d,%f\n",p->num,p->age,p->score);
p=p->next;
}while(p!=NULL);
}
return 0;
}

C语言链表创建

标签:动态链表   sizeof   int   ext   数据   def   scanf   include   \n   

原文地址:https://www.cnblogs.com/tudapao/p/9021055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!