我需要编写一个函数:无法更改该功能!
`void list_courses(),按顺序列出所有学生注册的所有课程号。我该如何解决这个问题?

最佳答案

您说我们不应该更改功能void list_courses(),但是必须在某个地方有一个struct student *才能开始遍历所有学生和所有课程。您似乎有一个静态变量的初始学生,我假设您以某种方式填充了该静态变量。这是我的方法:

static struct student *my_student;

void list_courses(){

    struct student *current_student = my_student;
    struct course *current_course;

    // Iterate over all students
    while(current_student != NULL){
        current_course = current_student->courses;

        // do something with the current student if needed ...

        // Iterate over courses of this student
        while(current_course != NULL){

            // do something with current course ...

            // Advance to the next course of this student
            current_course = current_course->next;
        }

        // Advance to the next student
        current_student = current_student->next;
    }
}


这段代码可能更紧凑,但是为了清楚起见,我做了一个详细的版本。

关于c - 列出链表的内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46920498/

10-12 07:25
查看更多