2016-11-15 53 views
0
#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; 
} 

我想創建一個列表爲例鏈表(嵌套鏈接列表)

Student list : 

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

這是我的結構的近似paiting內部列表,我趕到添加一個主題給一個學生,但我不知道如何添加更多。 在此先感謝。

我所定義的學生名單作爲一個全球性,因爲我會需要一個包含所有學生

+0

什麼是你的問題? –

+0

我編輯了我的問題 –

+0

什麼是模塊?它在學生裏面嗎? – Sean83

回答

0

讓這是你的學生只列出一個清單:

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

現在,如果你想添加模塊「計算機科學「,然後你必須執行以下步驟:

  1. 遍歷學生列表以找到student2。
  2. 然後遍歷它的模塊列表。
  3. 添加模塊「計算機科學」列出(您可以添加任何地方根據您的要求)。

你的功能將是這樣的:

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; 
    } 
} 
+0

如果我想在那個學生的創造增加學生的模塊 –

+0

創建學生和剛纔添加的模塊,在上述功能。在這種情況下,您不必遍歷學生列表,因爲您已經知道在哪裏添加模塊:p –

+0

還有一個問題,模塊指針是本地還是全局?我是否需要在主函數中傳遞指向模塊或學生的指針? :P –