1
 #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;
}

What I want to create is a list inside a list exemple

  Student list :

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

that's an approximate paiting of my structure, i arrived to add one subject to one student, but i don't know how to add more. thanks in advance.

I defined the student list as a global one since i would be needing only one list containing all students

9
  • What's your question? Commented Nov 15, 2016 at 19:13
  • i edited my question Commented Nov 15, 2016 at 19:17
  • what is module? is it inside of student? Commented Nov 15, 2016 at 19:40
  • traverse the student list and find the student to which you want to add the module, then just traverse its module list and add the module to its module list. Commented Nov 15, 2016 at 19:42
  • @rajev how ? sean i don't know if i get u well, but every student have a linked list of subjects. Commented Nov 15, 2016 at 19:44

1 Answer 1

1

Let this is your student list :

Student1 ==> student2 ==> NULL
   |            |
   maths        POO
   |            |
   physiques    English

Now if you want to add module "Computer Science" to student2 then you have to do the following steps:

  1. traverse the student list to find the student2.
  2. then traverse its module list.
  3. add module "Computer Science" to list(you can add anywhere as per your requirement).

Your function will be like this:

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;
    }
 }
Sign up to request clarification or add additional context in comments.

10 Comments

what if i want to add student's modules at the creating of that student
create the student and just add the modules as in the above function. You don't have to traverse student list in this case coz you already know where to add modules :p
one more question, module pointer is a local one or a global one ? do I need to pass a pointer to module or student in the main function ? :p
it should be local coz you don't need it outside the function in the above implementation. Only one global pointer is needed which will point to the start of the student list, here it is etudiant.
well, i did not really quite understand ur logic, and when i tried it my program crashed, it'd be good if u add some exemple names, thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.