首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链表中的链表(嵌套链表)

链表中的链表(嵌套链表)
EN

Stack Overflow用户
提问于 2016-11-15 19:13:12
回答 1查看 2.5K关注 0票数 1
代码语言:javascript
复制
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>



  struct module {

 char name[10];
  int note;
  struct module * next;

    };
  typedef struct module module;


  struct student {
  char name[10];
  char adress[20];
  struct student * next;
  module * head;

 } ;
 typedef struct student student;


 student *etudiant=NULL;



  module* add_module(char name[],int note){
module *p=(module*)malloc(sizeof(module));
p->note=note;
p->next=NULL;
strcpy(p->name,name);

return p;
 }



 void add_student(char name[], char adress[])

 {

     student *p=(student*)malloc(sizeof(student));
     strcpy(p->name,name);
     strcpy(p->adress,adress);

     p->head= add_module("algo",15);
     p->next=NULL;

     if (etudiant==NULL){

    etudiant=p;
}
else{
    student *q = etudiant;

while(q->next!=NULL){

    q=q->next;

     }
     q->next=p;
          }
 }


    void print_module(module *m){

 if (m==NULL)
 {
     printf("NULL");


 }
 else
 {
     while(m->next!=NULL){
         printf("%s   ",m->name);
    printf("%d\n",m->note);
    m=m->next;
     }

 }


}


 void print(){
 student *p;
 module *m;
 p = etudiant;
 if (etudiant==NULL){

printf("NULL");
 }

 else
 {
while (p->next!=NULL);
 {
     printf("%s   ",etudiant->name);
     printf("%s   ",etudiant->adress);

     m = p->head;
     while(m != NULL){
        printf("%s ",m->name);
        printf("%d ",m->note);
        m= m->next;
     }
     p = p->next;
      }
 }


 }

int main () {

    add_student("jack","nowhere");
    print();

    return 0;
}

我想要创建的是列表中的列表

代码语言:javascript
复制
  Student list :

  Student || subject || ==> student 2 || subject
          |                          |
          maths                      POO
          |                          |
          physiques                 English

这是我的结构的大致写法,我是来给一个学生加一门课的,但我不知道怎么多加一点。提前谢谢。

我将学生列表定义为全局列表,因为我只需要一个包含所有学生的列表。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-15 20:08:31

让这是你的学生名单:

代码语言:javascript
复制
Student1 ==> student2 ==> NULL
   |            |
   maths        POO
   |            |
   physiques    English

现在,如果要将“计算机科学”模块添加到student2中,则必须执行以下步骤:

  1. 遍历学生列表以找到student2。
  2. 然后遍历其模块列表。
  3. 添加模块“计算机科学”到列表(您可以添加任何地方,根据您的要求)。

您的功能如下:

代码语言:javascript
复制
typedef struct student student;
void addModule(char studentName[], char subject[], int note) {
    // searching student in the list..
    if(etudiant != NULL) {
        struct student *s = etudiant; //start of the list
        while(s && strcmp(s->name, studentName) != 0)
            s = s->next;

        if(s != NULL) {
            // creating module...
            module* new = (module*)malloc(sizeof(module));
            strcpy(new->name, subject);
            new->note = note;
            new->next = NULL;

            //adding module to the front of the module list of student s ...
            module* tmp = s->head;
            s->head = new;
            new->next = tmp;
        }
    }
}

void add_student(char name[], char adress[]) {
    student *p=(student*)malloc(sizeof(student));
    strcpy(p->name,name);
    strcpy(p->adress,adress);
    p->next=NULL;

    if (etudiant==NULL){
        etudiant=p;
    }
    else {
        struct student *q = etudiant;

        while(q->next!=NULL){
            q=q->next;
        }
        q->next=p;
    }
    addModule(p->name, "algo", 15);
}

 int main() {
    add_student("A", "XYZ");
    addModule("A", "CS", 1);
    addModule("A", "MECH", 1);

    add_student("B", "PQR");
    addModule("B", "DAA", 1);
    addModule("b", "SE", 1);

    //printing the list...
    student *q = etudiant;
    while(q != NULL) {
        module *p = q->head;
        printf("%s -> ", q->name);
        while(p != NULL) {
            printf("%s ", p->name);
            p = p->next;
        }
        printf("\n");
        q = q->next;
    }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40617894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档