#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

// 科目構造体:ten.h
struct t_rec {
	int kamoku[3];
};

// 学生情報構造体:gaku3.h
struct g_rec {
	char gakuban[9];
	char cls[5];
	char *pnamae;
	int sei;
	struct t_rec *pten;
	struct g_rec *pgnext;
};

struct g_rec *new_record(void)
{
	struct g_rec *pg;
	int i;

	pg = (struct g_rec *)malloc(sizeof (struct g_rec));
	pg->pnamae = (char *)malloc(20);
	pg->pten = (struct t_rec *)malloc(sizeof (struct t_rec));

	printf("学籍番号==>");
	scanf("%8s", pg->gakuban);
	printf("クラス==>");
	scanf("%4s", pg->cls);
	printf("名前==>");
	scanf("%19s", pg->pnamae);
	printf("性別==>");
	scanf("%d", &pg->sei);
	for (i = 0; i < 3; i++) {
		printf("点数%d==>", i+1);
		scanf("%d", &pg->pten->kamoku[i]);
	}

	return pg;
}

int main()
{
	struct g_rec *pghead = NULL;
	struct g_rec *pg;
	struct g_rec *pgnext;
	int i, num;

	printf("n件入力==>");
	scanf("%d", &num);
	for (i = 0; i < num; i++) {
		pgnext = new_record();
		pgnext->pgnext = NULL;
		if (pghead == NULL) {
			pghead = pgnext;
			continue;
		}
		for (pg = pghead; ; pg = pg->pgnext) {
			if (pg->pgnext == NULL) {
				pg->pgnext = pgnext;
				break;
			}
		}
	}

	// リストの表示
	printf("*** 表示 ***\n");
	for (pg = pghead; pg; ) {
		printf("学籍番号:%s\n", pg->gakuban);
		printf("クラス:%s\n", pg->cls);
		printf("氏名:%s\n", pg->pnamae);
		printf("性別:%s\n", (pg->sei == 1) ? "男" : "女");
		for (i = 0; i < 3; i++) {
			printf("点数%d:%d\n", i+1, pg->pten->kamoku[i]);
		}
		pg = pg->pgnext;
	}

	// リストの削除
	for (pg = pghead; pg; pg = pgnext) {
		pgnext = pg->pgnext;
		free(pg->pnamae);
		free(pg->pten);
		free(pg);
	}

	return 0;
}
