首页 > 其他 > 详细

使用尾插法创建链表并且输出

时间:2020-08-18 20:24:05      阅读:91      评论:0      收藏:0      [点我收藏+]
//使用尾插法创建链表并且输出 
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define LEN sizeof(struct Student)
struct Student{
	long long num;
	float score;
	struct Student *next;
};
int n;
//建立创建链表的函数
struct Student *creat(){
	struct Student *head,*p1,*p2;
	p1=p2=(struct Student*)malloc(LEN);
	scanf("%ld%f",&p1->num,&p1->score);
	head=NULL;
	n=0; 
	while(p1->num!=0){
		n=n+1;
		if(n==1) head=p1;
		else p2->next=p1;
	    p2=p1;
		p1=(struct Student*)malloc(LEN);
		scanf("%ld%f",&p1->num,&p1->score); 
	}
	p2->next=NULL;
	return head;
}
void print(struct Student *head){
	struct Student *p;
	printf("输出信息:\n");
	p=head;
	while(p!=NULL){
		printf("%ld   %f\n",p->num,p->score);
		p=p->next;
	}
}
int main()
{
	struct Student *pt;
	pt=creat();
	print(pt);
	return 0;
}

 收录于文章《885程序设计考点狂背总目录中

使用尾插法创建链表并且输出

原文:https://www.cnblogs.com/byczyz/p/13525603.html

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