【数据结构与算法分析】3.27

多重表

在这里插入图片描述


P.S. 每个结点包括四个域:

  1. 于该结点关联的学生编号
  2. 与该节点关联的班级编号
  3. 该节点的下一个学生结点
  4. 该节点的下一个班级结点

模拟代码如下:

/*
输入:
第一行为学生数量n,之后有n组数据
第i组数据包括两行
第一行表示学生i选了x门课程
第二行有x个数,表示该学生所选课程编号。
输出:
输出每个班级注册的学生编号
若某班级无学生注册则不输出
*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
struct node
{
	int s, c;
	struct node *NextStudent;
	struct node *NextClass;
};
typedef struct node* List;
void Insert(List s, List c, int x, int y)
{
	List p = s;
	while (p->NextClass != s)
		p = p->NextClass;
	p->NextClass = (List)malloc(sizeof(struct node));
	p->NextClass->c = y;
	p->NextClass->NextClass = s;
	p = c;
	while (p->NextStudent != c)
		p = p->NextStudent;
	p->NextStudent = (List)malloc(sizeof(struct node));
	p->NextStudent->s = x;
	p->NextStudent->NextStudent = c;
}
void PrintClass(List L)
{
	List p = L;
	while (p->NextStudent != L)
	{
		printf("%d ", p->NextStudent->s);
		p = p->NextStudent;
	}
	printf("\n");
}
void main()
{
	int n, i, j, x, c, maxc = 0;
	List S[4000] = { NULL }, C[2500] = { NULL };
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
		S[i] = (List)malloc(sizeof(struct node));
		S[i]->NextClass = S[i];
		scanf("%d", &x);
		for (j = 1; j <= x; j++)
		{
			scanf("%d", &c);
			if (C[c] == NULL)
			{
				C[c] = (List)malloc(sizeof(struct node));
				C[c]->NextStudent = C[c];
			}
			if (c > maxc)
				maxc = c;
			Insert(S[i], C[c], i, c);
		}
	}
	for (i = 1; i <= maxc; i++)
		if (C[i] != NULL)
		{
			printf("Class:%d\n", i);
			PrintClass(C[i]);
		}
	getchar();
	getchar();
	return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值