如何创建指向另一个链接列表的链接列表?

时间:2018-06-16 20:23:05

标签: c linked-list

我正在执行此计划来管理注册每个科目的学生。 该系统由主题链表和学生链表组成。每个科目都有一个链接的学生指针列表。 每个学生都有一个指向各自科目的指针列表。 链接列表都没有标题。 我试图创建函数来构建指向每个学生的主题的指针列表,但我不明白我该如何创建它。

我到目前为止的结构是:

typedef struct node_subject * ListSubjects;
typedef struct node_subject {
    char *name;
    struct node_ptrStudent *Students;
    ListSubjects next;
}NodeSubject;

typedef struct node_student * ListStudents;
typedef struct node_student {
    char *name;
    int num;
    struct node_ptrSubject *Subjects;
    ListStudents next;
}NodeStudent;

typedef struct node_ptrSubject * ListPtrSubjects;
typedef struct node_ptrSubject {
    ListSubjects subjects;
    ListPtrSubjects next;
}ListPtrSubjects;

typedef node_ptrStudent * ListPtrStudents;
typedef struct node_ptrStudent {
    ListStudents student;
    ListPtrStudents next;
}ListPtrStudents;

void createListPtrSubjects (ListSubjects);

1 个答案:

答案 0 :(得分:0)

Linux内核风格说"将typedef用于结构和指针是错误的。" https://www.kernel.org/doc/html/v4.10/process/coding-style.html#typedefs;无论是否同意这一点,我认为你必须有很多typedef;这有点令人困惑。

如果您正在编写一个关系数据库,您可以使用它,这将是一个关联实体;见https://en.wikipedia.org/wiki/Associative_entity。另外,请确保您的主题有主键。

例如,

#include <stdlib.h> /* EXIT_ realloc */
#include <limits.h> /* INT_MAX */
#include <errno.h>  /* errno */
#include <stdio.h>  /* perror printf */
#include <assert.h> /* assert */

struct Subject {
    int id; /* Primary key. */
    const char *name;
    struct Takes *takes_head;
};

struct Student {
    int id; /* Primary key. */
    const char *name;
    struct Takes *takes_head;
};

/* (Weak) Associative entity. */
struct Takes {
    int subject_id, student_id; /* Compound key. */
    struct Takes *next_subject, *next_student;
};

static struct School {
    struct Subject *subjects;
    int subjects_no;
    struct Student *students;
    int students_no;
} school;

static struct Subject *lookup_subject(const int id) {
    if(id >= school.subjects_no) return 0;
    return school.subjects + id;
}

static struct Subject *add_subject(const char *name) {
    struct Subject *subjects, *s;
    assert(name);
    if(school.subjects_no == INT_MAX) { errno = ERANGE; return 0; }
    /* fixme: realloc is slow; eg, double capacity. */
    if(!(subjects = realloc(school.subjects,
        sizeof *school.subjects * (school.subjects_no + 1)))) return 0;
    school.subjects = subjects;
    s = school.subjects + school.subjects_no;
    s->id = school.subjects_no;
    s->name = name;
    s->takes_head = 0;
    school.subjects_no++;
    return s;
}

/* May output in a static buffer. */
static const char *subject_to_string(const int id) {
    struct Subject *const subject = lookup_subject(id);
    static char str[256] = ""; /* Static buffer. */
    if(!subject) { sprintf(str, "<subject id %d>", id); return str; }
    return subject->name;
}

static struct Student *lookup_student(const int id) {
    if(id >= school.students_no) return 0;
    return school.students + id;
}

static struct Student *add_student(const char *name) {
    struct Student *students, *s;
    assert(name);
    if(school.students_no == INT_MAX) { errno = ERANGE; return 0; }
    if(!(students = realloc(school.students,
        sizeof *school.students * (school.students_no + 1)))) return 0;
    school.students = students;
    s = school.students + school.students_no;
    s->id = school.students_no;
    s->name = name;
    s->takes_head = 0;
    school.students_no++;
    printf("%s is assigned id %d.\n", s->name, s->id);
    return s;
}

/* May output in a static buffer. */
static const char *student_to_string(const int id) {
    struct Student *const student = lookup_student(id);
    static char str[256] = ""; /* Static buffer. */
    if(!student) { sprintf(str, "<student id %d>", id); return str; }
    return student->name;
}

static struct Takes *lookup_takes(const int student_id, const int subject_id) {
    const struct Student *const student = lookup_student(student_id);
    struct Takes *t;
    if(!student) return 0;
    for(t = student->takes_head; t && t->subject_id != subject_id; t = t->next_student);
    return t;
}

static struct Takes *add_takes(const int student_id, const int subject_id) {
    struct Subject *const subject = lookup_subject(subject_id);
    struct Student *const student = lookup_student(student_id);
    struct Takes *t = lookup_takes(student_id, subject_id);
    printf("%s enrols in %s.\n", student_to_string(student_id),
        subject_to_string(subject_id));
    /* Already have it. */
    if(t) return t;
    /* Or else make a new. */
    if(!subject || !student) { errno = EDOM; return 0; }
    if(!(t = malloc(sizeof *t))) return 0;
    t->subject_id = subject_id;
    t->student_id = student_id;
    t->next_subject = subject->takes_head, subject->takes_head = t;
    t->next_student = student->takes_head, student->takes_head = t;
    return t;
}

static void print_subject(const int subject_id) {
    struct Subject *const subject = lookup_subject(subject_id);
    struct Takes *t;
    printf("_Subject: %s._\n", subject_to_string(subject_id));
    if(!subject) return;
    for(t = subject->takes_head; t; t = t->next_subject)
        printf("%s takes %s.\n", student_to_string(t->student_id),
        subject_to_string(t->subject_id));
}

static void print_student(const int student_id) {
    struct Student *const student = lookup_student(student_id);
    struct Takes *t;
    printf("_Student: %s._\n", student_to_string(student_id));
    if(!student) return;
    for(t = student->takes_head; t; t = t->next_student)
        printf("%s takes %s.\n", student_to_string(t->student_id),
            subject_to_string(t->subject_id));
}

int main(void) {
    const char *why = 0;
    do { /* Try. */
        struct Subject *subject;
        struct Student *student;
        int herbology_id, defense_id, potions_id;
        int hermione_id, ron_id, harry_id;

        why = "Herbology";
        if(!(subject = add_subject(why))) break;
        herbology_id = subject->id;
        why = "Defense";
        if(!(subject = add_subject(why))) break;
        defense_id = subject->id;
        why = "Potions";
        if(!(subject = add_subject(why))) break;
        potions_id = subject->id;
        why = "Hermione";
        if(!(student = add_student(why))) break;
        hermione_id = student->id;
        why = "Ron";
        if(!(student = add_student(why))) break;
        ron_id = student->id;
        why = "Harry";
        if(!(student = add_student(why))) break;
        harry_id = student->id;
        why = "enrol";
        if(!add_takes(hermione_id, defense_id)
            || !add_takes(hermione_id, potions_id)
            || !add_takes(hermione_id, herbology_id)
            || !add_takes(ron_id, herbology_id)
            || !add_takes(harry_id, potions_id)) break;
        print_subject(herbology_id);
        print_subject(defense_id);
        print_subject(potions_id);
        print_student(hermione_id);
        why = 0;
    } while(0); if(why) perror(why); /* Catch. */
    /* fixme: free memory in 'final' block. Remember to free Takes. */
    return why ? EXIT_FAILURE : EXIT_SUCCESS;
}