首页 > 编程语言 > 详细

C语言链表创建

时间:2018-05-10 19:14:53      阅读:234      评论:0      收藏:0      [点我收藏+]

#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语言链表创建

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!