簡體   English   中英

C(鏈表)中的結構內部結構

[英]Structure inside structure in C (Linked List)

我正在嘗試編寫一個程序,該程序允許我按順序打印教師的信息,包括學生 ID。 例如:當我嘗試運行程序時,output 將類似於以下內容:

Teacher Apple, Age 55:
Student ID: 77
Student ID: 5456
Student ID: 4729
Student ID: 3784

Teacher bob, Age 67:
Student ID: 477
Student ID: 546
Student ID: 429
Student ID: 784

.. 等等。

但是,我想不出一種以正確格式打印它們的方法。 我在使用嵌套循環時遇到問題。 這是我的代碼:

struct Teacher {

    char* name;
    int age;
    struct Student* stds;
    struct Teacher* next;
};

struct Student 
{
    int id;
    struct Student* next;
};


int main() {

    //aray to store teachers' names
    char names[3][10] = { "first", "second", "third" };
//their age
    int age[3] = { 1,2,3 };
//students' id
    int id[4] = { 1, 2, 3, 4 };
//alocate the linkedlist in mem
    struct Teacher* head = (struct Teacher*)malloc(sizeof(struct Teacher));
    struct Student* headstd = (struct Student*)malloc(sizeof(struct Student));
    
    
    head->next = NULL;
    headstd->next = NULL;
    //head ->next = (struct Teacher*)malloc(sizeof(struct Teacher));
//adding temporary head and modefied it so I don't touch the actual head 
    struct Teacher* temhead = (struct Teacher*)malloc(sizeof(struct Teacher));
    struct Student* stdhead = (struct Student*)malloc(sizeof(struct Student));
    //head->name = 
    //(char)first.name ="first";
    temhead->next = NULL;
    stdhead->next = NULL;
///loop thorough the teachers
    for (int i = 0; i < 3; i++) {
//loop through the students 
        for (int j = 0; j < 4; j++) {
            stdhead->next = (struct Student*)malloc(sizeof(struct Student));
            stdhead = stdhead->next;
            stdhead->id = *(id + j);
            //temhead->age = 44;
            
            printf("Student ID: %d \n ", stdhead->id);
        }
//here after it goes through the students, it goes through teachers 
        temhead->next = (struct Teacher*)malloc(sizeof(struct Teacher));
        temhead = temhead->next;
//here I assigned the name of teachers to the array I have implementd @ first (names[3][10]
        temhead->name = *(names + i);
//print names (for now "first, second, ..")
        printf("node: %s \n ", temhead->name);

    }
//
//  for (int i = 0; i < 3; i++) {
//      temhead->next = (struct Teacher*)malloc(sizeof(struct Teacher));
//      //temhead->stds = (struct Teacher*)malloc(sizeof(struct Teacher));
//      temhead = temhead->next;
//      temhead->age = *(age + i);
//      //temhead->age = 44;
//      printf("%d \n ", temhead->age);
//
//  }
//
//  
//}

我剛開始學習 C,我真的很感激任何幫助。 我的輸出如下所示: 在此處輸入圖像描述

打印代碼可能看起來像這樣。 但是,除非您首先正確地組裝教師和學生,否則它不會起作用。

for (struct Teacher *t = head; t != NULL; t = t->next)
{
    printf("Teacher %s, Age %d:\n", t->name, t->age);
    for (struct Student *s = t->stds; s != NULL; s = s->next)
        printf("Student ID: %d\n", s->id);
}

為了組合這些列表,您可能需要編寫一些函數來創建教師並將其添加到列表中,並將學生添加到教師中。 然后首先調用這些函數來組裝列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM