C语言单链表简单的输入输出,输入学生学号,姓名,输出;

C语言单链表简单的输入输出,输入学生学号,姓名,输出;,第1张

#include 
#include
typedef struct student
{
	int num;
	char name[10];
	struct student *next;
}str;
int main()
{
	str *p1=NULL;
	str *p2=NULL;
	str *head=NULL;
	p1=(str*)malloc(sizeof (struct student));
	printf("input num & name:\n");
	scanf("%d",&(p1->num));
	scanf("%s",p1->name);
	head=p1;
	while(p1->num!=0)
	{
		p2=p1;
		p1=(str*)malloc(sizeof (struct student));
		printf("input num & name:\n");
		scanf("%d",&(p1->num));
		if(p1->num==0)
		{
			p2->next=p1;
			break;
		}
		else
		{
			scanf("%s",p1->name);
			p2->next=p1;
		}
		
	}
	p2->next=NULL;
	free(p1);
	p1=NULL;
	while(head!=NULL)
	{
		printf("%d %s\n",head->num,head->name);
		head=head->next;
	}
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/707056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存