C语言实现链表尾插法

#include<stdio.h>
#include<stdlib.h>
/*
01 构建链表
02 初始化链表  
03 输出链表 

*/

typedef struct stu{
	int num;
	float score;
	 struct stu *next;
}STU,*PSTU;

PSTU create(){
	PSTU head, p1, p2;
	p2 = p1 = (PSTU)malloc(sizeof(STU));
    //head = NULL;

	printf("请输入节点编号:\n");
	scanf("%d",&p1->num) ;
	int n=0;
	while (p1->num != 0){
		    n++;
			printf("请输入节点num为%d的分数:\n",p1->num);
	        scanf("%f",&p1->score) ;
	        
	        if(n==1)
	        	head = p1;
	        else {
	
	           p2->next = p1; //指向尾节点 
	          
			   	}
			p2 = p1; //保持 p2一直是尾节点, p1是新加入节点 
	        p1 = (PSTU)malloc(sizeof(STU));
	        printf("请输入节点编号:\n");
	        scanf("%d",&p1->num) ;
	        
	}
	p2->next = NULL;
	return head;
}

void print(PSTU p){
	PSTU pstu;
	pstu = p;
	do{
	printf("stu number: %d , score:%f\n",pstu->num, pstu->score);
           pstu =	pstu->next; //head->next = head
}while(pstu != NULL);


}

int main(){

PSTU pstu;

pstu = create();
print(pstu);

return 0;
} 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值