- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做这个项目来管理每个科目的学生。该系统由科目链表和学生链表组成。每个科目都有一个供学生引用的链接列表。每个学生都有一份指向各自科目的指针列表。所有链接列表都没有标题。我试图创建一个函数来构建指向每个学生科目的指针列表,但我不明白如何创建它。
到目前为止我拥有的结构是:
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/
有人能给我解释一下我下面程序中的一点吗我编写的以下程序是为了理解 int * const * var 的含义; #include "iostream" using namespace std ; in
我正在摆弄 C 中的指针,但仍然不确定一些非常基础的知识。我想出了以下示例代码: #include int main(void) { int num = 42; /
以下代码产生警告: const char * mystr = "\r\nHello"; void send_str(char * str); void main(void){ send_str
我正在尝试在我的 Linux 计算机上升级 cmake。这是我使用的命令。 删除旧的 cmake: sudo apt purge --auto-remove cmake 从 https://cmake
我试图借助 char 指针来追踪 union 第一个字节的值。我假设 byte1 的输出应该是 ff,但实际上是 ffffffff。如果我错了请纠正我。 #include #include #in
我正在使用微 Controller 进行一些 ADC 测量。当我尝试使用 -O2 优化编译以下代码时遇到问题,当代码中存在 PrintVal() 函数时,MCU 卡住。我做了一些调试,结果发现,当我添
#include #include using namespace std; int main () { vector qwerty; qwerty.push_back(5);
我有我的 woking setup.py 文件;其中包含以下行: home = os.environ["HOME"] # home = /home/dr in my machine; distu
这段代码 #include #include static_assert(std::is_same_v::value_type, volatile int>); 在最新的 GCC 和 clang
我对 C 中的前向声明有疑问。 代码 typedef struct yhash_s t_yhash;// forward declaration struct yhash_s { size_t
我想提取成员指针指向的成员的类型。 template void demo(myClass& instance, void* ptr) { instance.*member = *reinter
我正在尝试使用指针将一段 C 代码转换为 Swift 3。这是 C 代码中的相关部分。 Float32 sampleArray[256] = { // Array is 256 Float value
您可能知道,VLA's haves pros and cons 和它们在 C11 中是可选的。 我想使 VLA 成为可选项的主要原因是:“堆栈可能会爆炸”: int arr[n]; /* where
这段代码有什么错误?为什么我不能按照我尝试的方式取消引用该元素。 #include typedef struct { int value; struct node * left;
有什么方法可以在 JavaScript 中创建\返回指向变量的指针吗? 比如,在 PHP 中: function func() { ..... return &$result; } 我
如果您想使用方法的指针作为参数,则需要将该方法键入作为对象的函数,就像这样好 : type TAcceptor = function(filename:string):boolean of objec
很简单的问题: 我对 C++ 中的智能指针有点陌生。我想我得到了所有权的东西,但我不知道如何访问他们实际指向的内容。当我尝试使用对象的成员函数/变量时,我只是得到了 unique_ptr 类的函数,这
我得到了一个点的方位 Angular 、指南针方向和一个可以将箭头设置到某个方向的 api(0 是顶部,90 是右侧,180 是底部,360 是顶部) 如果我希望箭头指向我采用方位 Angular 形
我正在尝试找到一种方法,从单元格中获取位于当前工作表左侧(托盘下方)的工作表单元格中的数据。 我知道如何通过调用其他工作表 =Sheet1!A1 但现在我需要一些最好的解释 =Sheet[-1]!A1
所以我在 MATLAB 中有一个 for 循环,其中向量 x 将通过一个函数,比如 cos(x).^2,或者不同的选择,比如 sin(x).^2 + 9.*x。用户将在 for 循环之前选择他想使用的
我是一名优秀的程序员,十分优秀!