-2
我需要C.利用嵌套链接列表Ç
struct subject
{
char *subj[100];
char *tutor[100];
char *mark[10];
char *date[20];
struct subject* next2;
}*head2;
struct student
{
char *name[20];
char *surname[20];
int num;
struct subject *head2;
struct student* next1;
}*head1;
创建链表里面链表我有这两种结构。我尝试使用文件在循环中添加像这样的新节点。
void newstudent(FILE *fp, char buffor[255], char buffor1[255])
{
struct student *temp1;
temp1 = (struct student*) malloc(sizeof(struct student));
struct subject *temp2;
temp2 = (struct subject*) malloc(sizeof(struct subject));
fscanf(fp, "%s", temp1->name);
fscanf(fp, "%s", temp1->surname);
fscanf(fp, "%d", &temp1->num);
strcpy(temp2->subj, buffor);
strcpy(temp2->tutor, buffor1);
fscanf(fp, "%s", temp2->mark);
fscanf(fp, "%s", temp2->date);
temp1->next1 = NULL;
head1 = temp1;
temp2->next2 = NULL;
head2 = temp2;
}
而当我只需要添加主题。
struct subject *temp2;
temp2 = (struct subject*) malloc(sizeof(struct subject));
strcpy(temp2->subj, buffor);
strcpy(temp2->tutor, buffor1);
printf("%s %s\n",temp2->subj,temp2->tutor);
fscanf(fp, "%s", temp2->mark);
fscanf(fp, "%s", temp2->date);
temp2->next2 = head2;
head2 = temp2;
我有问题,当我尝试显示“主题”列表中的元素。
while(link != NULL)
{
fprintf(output, "%s %s\n", link->name,link->surname);
fprintf(output, "Number: %d\n", link->num);
struct subject *link2 = head2;
while (link2 != NULL)
{
fprintf(output, "%s\n", link2->subj);
}
link = link->next1;
}
我认为这是实现嵌套列表的问题。
未定义的行为嘉豪。打开编译器警告,阅读它们,修复它们,再试一次。 – EOF
学生真的有20个名字指针和第二个名字指针的数组吗?或者你打算'char name [20];'而不是'char * name [20];'?与其他结构类似。为什么哦为什么你没有尝试一个简单的案例,并通过微小的可证明的步骤来构建代码步骤? –