C语言链表的输入输出

1.学生成绩的输入输出

#include <stdio.h>
#include <stdlib.h>
#define len sizeof(student)
typedef struct students
{
	char name[20];
	int num;
	float score;
	struct students *next;
}student;
int n;
int main()
{
	student *lianbiao(void);
	void print(student *p);
	student *head;
	head = lianbiao();
	print(head);
	system("pause");
	return 0;
}
student *lianbiao(void)
{
	student *head, *p1, *p2;
	n = 0;
	p1 = p2 = (student*) malloc(len);
	head = NULL;
	scanf_s("%s %d %f", p1->name,20,&p1->num, &p1->score);
	while (p1->num != 0)
	{
		n = n + 1;
		if (n == 1) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 =(student*) malloc(len);
		scanf_s("%s %d %f", p1->name, 20,&p1->num, &p1->score);
	}
	p2->next = NULL;
	return head;
}
void print(student *head)
{
	student *p=head;
	printf("Having %d student:\n", n);
	if (head != NULL)
	{
		printf("Name      number  score\n");
		while (p != NULL)
		{
			printf("%-10s%-8d%-5.2f\n", p->name, p->num, p->score);
			p = p->next;
		}
	}
}

#include <stdio.h>
#include <stdlib.h>
#define len  sizeof(preson)
union clas_position
{
	int clas;
	char position[20];
};
typedef struct presons
{
	int num;
	char name[20];
	char sex;
	char job[20];
	union clas_position category;
	struct presons *next;
}preson;
int n;
int main()
{
	preson *lianbiao(void);
	void print(preson *head);
	preson *head;
	head = lianbiao();
	print(head);
	system("pause");
	return 0;
}
preson *lianbiao(void)
{
	preson *head, *p1, *p2;
	n = 0;
	p1 = p2 = (preson*)malloc(len);
	head = NULL;
	scanf_s("%d %s %c", &p1->num, p1->name, 20, &p1->sex);
	scanf_s("%s", p1->job,20);
	if (p1->job[0] == 's' || p1->job[0] == 'S')
		scanf_s("%d", &p1->category.clas);
	else if (p1->job[0] == 't' || p1->job[0] == 'T')
		scanf_s("%s", p1->category.position, 20);
	else printf("input error!");
	while (p1->num!= 0)
	{
		n = n + 1;
		if (n == 1) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (preson*)malloc(len);
		scanf_s("%d %s %c", &p1->num, p1->name, 20,&p1->sex);
		scanf_s("%s", p1->job,20);
		if (p1->job[0] == 's' || p1->job[0] == 'S')
			scanf_s("%d", &p1->category.clas);
		else if (p1->job[0] == 't' || p1->job[0] == 'T')
			scanf_s("%s", p1->category.position, 20);
		else printf("input error!");
	}
	p2->next = NULL;
	return head;
}
void print(preson *head)
{
	preson *p = head;
	printf("Having %d persons:\n", n);
	if (head != NULL)
	{
		printf("NO.        name       sex  job       class/position\n");
		while (p != NULL)
		{
			printf("%-10d %-10s %-4c %-10s", p->num, p->name, p->sex, p->job);
			if (p->job[0]== 's' || p->job[0] == 'S')
				printf("%-8d\n", p->category.clas);
			else if (p->job[0] == 't' || p->job[0] == 'T')
				printf("%-8s\n", p->category.position);
			p = p->next;
		}
	}
}

2.学生与老师信息的输入(共用体)

#include <stdio.h>
#include <stdlib.h>
#define len  sizeof(preson)
union clas_position
{
	int clas;
	char position[20];
};
typedef struct presons
{
	int num;
	char name[20];
	char sex;
	char job[20];
	union clas_position category;
	struct presons *next;
}preson;
int n;
int main()
{
	preson *lianbiao(void);
	void print(preson *head);
	preson *head;
	head = lianbiao();
	print(head);
	system("pause");
	return 0;
}
preson *lianbiao(void)
{
	preson *head, *p1, *p2;
	n = 0;
	p1 = p2 = (preson*)malloc(len);
	head = NULL;
	scanf_s("%d %s %c", &p1->num, p1->name, 20, &p1->sex);
	scanf_s("%s", p1->job,20);
	if (p1->job[0] == 's' || p1->job[0] == 'S')
		scanf_s("%d", &p1->category.clas);
	else if (p1->job[0] == 't' || p1->job[0] == 'T')
		scanf_s("%s", p1->category.position, 20);
	else printf("input error!");
	while (p1->num!= 0)
	{
		n = n + 1;
		if (n == 1) head = p1;
		else p2->next = p1;
		p2 = p1;
		p1 = (preson*)malloc(len);
		scanf_s("%d %s %c", &p1->num, p1->name, 20,&p1->sex);
		scanf_s("%s", p1->job,20);
		if (p1->job[0] == 's' || p1->job[0] == 'S')
			scanf_s("%d", &p1->category.clas);
		else if (p1->job[0] == 't' || p1->job[0] == 'T')
			scanf_s("%s", p1->category.position, 20);
		else printf("input error!");
	}
	p2->next = NULL;
	return head;
}
void print(preson *head)
{
	preson *p = head;
	printf("Having %d persons:\n", n);
	if (head != NULL)
	{
		printf("NO.        name       sex  job       class/position\n");
		while (p != NULL)
		{
			printf("%-10d %-10s %-4c %-10s", p->num, p->name, p->sex, p->job);
			if (p->job[0]== 's' || p->job[0] == 'S')
				printf("%-8d\n", p->category.clas);
			else if (p->job[0] == 't' || p->job[0] == 'T')
				printf("%-8s\n", p->category.position);
			p = p->next;
		}
	}
}



  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值