动态链表

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student{ //相当于结点
int num; //两个数据域
float score;
struct student *next; //一个指针域
};
int n;

struct student *creat( )//动态创建链表的子函数
{ struct student *head,*p1,p2; n=0;
p1=p2=( struct student
) malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;

while(p1->num!=0)// 循环体
{n=n+1;//n作为计数器
if(n==1)head=p1;//链接的直接就是第一个结点 head p1 p2 都指向第一个结点
else p2->next=p1;//否则 已经有结点的话 就只需要把p1链接到p2的后面 每次输入的数据都是用p1存储

p2=p1;// 每次把p2移到当前链表的最后一个位置

p1=(struct student*)malloc(LEN); //创建下一个结点
scanf("%ld,%f",&p1->num,&p1->score);}//循环结束
//同时输入此节点的数据
p2->next=NULL; return(head);
}
int main( )
{ struct student *head,p;//链表自然要定义头指针,指针自然带
head=creat( );
p=head;
do{ //
printf("%ld %5.1f\n",p->num,p->score);//由p做过渡,逐个输出结点元素值
p=p->next;
}while(p!=NULL);//套用 do while 循环 全部输出
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

约卡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值