gpt4 book ai didi

c - 如何创建一个指向另一个链表的链表?

转载 作者:行者123 更新时间:2023-11-30 16:30:51 26 4
gpt4 key购买 nike

我正在做这个项目来管理每个科目的学生。该系统由科目链表和学生链表组成。每个科目都有一个供学生引用的链接列表。每个学生都有一份指向各自科目的指针列表。所有链接列表都没有标题。我试图创建一个函数来构建指向每个学生科目的指针列表,但我不明白如何创建它。

到目前为止我拥有的结构是:

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

关于c - 如何创建一个指向另一个链表的链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50891476/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com