多重表
P.S. 每个结点包括四个域:
- 于该结点关联的学生编号
- 与该节点关联的班级编号
- 该节点的下一个学生结点
- 该节点的下一个班级结点
模拟代码如下:
/*
输入:
第一行为学生数量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;
}