欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C语言链表的输入输出

程序员文章站 2024-03-23 21:10:16
...

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;
		}
	}
}
C语言链表的输入输出

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;
		}
	}
}

C语言链表的输入输出