嵌套链表C.

时间:2017-02-22 20:58:25

标签: c

我需要在C中的链表中创建链表。

remote_command=$(printf '%q ' ./myscript '$HOME')
ssh somehost "$remote_command"

我有这两种结构。我尝试使用file在循环中添加这样的新节点。

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;

我认为这是实现嵌套列表的问题。

1 个答案:

答案 0 :(得分:1)

循环:

while (link2 != NULL)
{
    fprintf(output, "%s\n", link2->subj);
}

如果link2不为null,则无限。在循环内部,必须是一个在某个时刻分配NULL的语句,以使循环结束。