建立动态链表

遇到0就结束,并且输出第一个人的成绩

讲解:
1.首先建立结构体:

struct student{
    int num;
    float cj;
    struct student *next;
};

2.用三个结构体指针(head,p1,p2)和开辟的动态存储区(mly)存储数据

让p1,p2都指向第一次开辟的动态存储区(mly);head存为NULL;

第一次输入数据后,n=1,将该地址存给head;

之后n不再等于1;

将p1指向第二次开辟的动态存储区(mly),p2依旧指向第一个mly,等第二个mly数据输入完成后,让p2指向第二个mly,让p1指向第三次开辟的动态存储区(mly);以此类推

 

#include<stdio.h>
#include<stdlib.h> 
#define mly sizeof(struct student)
struct student{
	int num;
	float cj;
	struct student *next;
};
int n;
struct student *fgo()
{
	struct student *head,*p1,*p2;
	n=0;
	p1=p2=(struct student*)malloc(mly);
	scanf("%d %f",&p1->num,&p1->cj);
	head=NULL;
	while(p1->num!=0)
	{
		n+=1;
		if(n==1)
		head=p1;
		else
		p2->next=p1;
		p2=p1;
		p1=(struct student *)malloc(mly);
		scanf("%d %f",&p1->num,&p1->cj);
	}
	p2->next=NULL;
	return(head);
}
int main()
{
	struct student *pt;
	pt=fgo();
	printf("\nnum:%d\n成绩:%5.1f\n",pt->num,pt->cj);
}

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值