簡體   English   中英

如何創建指向另一個鏈表的鏈表?

[英]How do i create a linked list that points to another 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);

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

暫無
暫無

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

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