我写了这段代码:(上面的结构和下面的函数) 我正在尝试这个功能来注册学生并将他删除。
void reg_student(slist *students, clist *courses, int id, int number){
slist *temp;
clist*temp1;
student *reg;
course *addit;
for (temp = students; temp->info->id!=id; temp = temp->next);
reg = temp->info;// the student we want to register.
for (temp1 = courses; temp1->info->number != number; temp1 = temp1->next);
addit = temp1->info;//the course to add.
//adding the course to the student courses list.
reg->courses = add_course(reg->courses, addit->title, addit->number);
//adding the student to the course students list.
addit->students = add_student(addit->students, reg->name, reg->id);
void unreg_student(slist *students, int id, int number){
slist *temp;
clist *temp1;
student *studenttoremove;
course *coursetoremove;
for (temp = students; temp->info->id != id; temp = temp->next);
studenttoremove = temp->info;// the student we want to delete.
for (temp1 = studenttoremove->courses; temp1->info-> number != number; temp1 = temp1->next);
coursetoremove = temp1->info;//the course we want to delete.
studenttoremove->courses = remove_course(studenttoremove->courses, coursetoremove);
temp1->info->students = remove_student(temp1->info->students, studenttoremove);
}
slist* remove_student(slist*students, student* s1){
slist* temp=students,*temp1;
if (temp->info == s1){
if (!(temp->next)){
free(temp);
return NULL;
}
temp1 = temp->next;
free(temp);
return temp1;
}
for (temp = students; temp->next->info != s1; temp = temp->next);
temp1 = temp->next;
temp = temp1->next;
free(temp1);
return students;
}
clist* remove_course(clist*courses, course * c1){
clist* temp=courses, *temp1;
if (temp->info == c1){
if (!(temp->next)){
free(temp);
return NULL;
}
temp1 = temp->next;
free(temp);
return temp1;
}
for (temp = courses; temp->next->info != c1; temp = temp->next);
temp1 = temp->next;
temp = temp1->next;
free(temp1);
return courses;
}
`结构是:
typedef struct course {
char *title;
int number;
struct slist *students;
} course;
typedef struct slist {
student *info;
struct slist *next;
} slist;
typedef struct clist {
course *info;
struct clist *next;
} clist;
这些功能应该添加和删除学生和课程。 寄存器工作正常。但是当我试图删除学生时,该课程将从学生课程列表中删除,但学生不会从课程列表中删除。当我调试它时,我看到删除功能正在获取一个NULL作为学生列表..我不明白为什么。 谢谢。
答案 0 :(得分:0)
这两个功能的主要问题是在最后,你有temp = temp1->next
。它应该是temp->next = temp1->next
。
slist* remove_student(slist*students, student* s1){
slist* temp=students,*temp1;
if (temp->info == s1){
if (!(temp->next)){
free(temp);
return NULL;
}
temp1 = temp->next;
free(temp);
return temp1;
}
for (temp = students; temp->next->info != s1; temp = temp->next);
if (!temp->next)
return students;
temp1 = temp->next;
temp->next = temp1->next;
free(temp1);
return students;
}
clist* remove_course(clist*courses, course * c1){
clist* temp=courses, *temp1;
if (temp->info == c1){
if (!(temp->next)){
free(temp);
return NULL;
}
temp1 = temp->next;
free(temp);
return temp1;
}
for (temp = courses; temp->next->info != c1; temp = temp->next);
if (!temp->next)
return courses;
temp1 = temp->next;
temp->next = temp1->next;
free(temp1);
return courses;
}
编辑:抱歉,我完全错过了。另一个问题出在unreg_student()
本身。原因是当调用remove_student()
时,您将temp1->info->student
作为第一个参数传递,而temp1
指向的内存块已被先前调用的remove_course()
释放。这意味着您正在玩内存访问冲突。 temp1
中存储的内容可能不再是您所期望的,甚至可能无法访问。但是你可以很容易地解决这个问题。只需将unreg_student()
的最后一行替换为coursetoremove->students = remove_student(coursetoremove->students, studenttoremove);
:
void unreg_student(slist *students, int id, int number){
slist *temp;
clist *temp1;
student *studenttoremove;
course *coursetoremove;
for (temp = students; temp->info->id != id; temp = temp->next);
studenttoremove = temp->info;// the student we want to delete.
for (temp1 = studenttoremove->courses; temp1->info->number != number; temp1 = temp1->next);
coursetoremove = temp1->info;//the course we want to delete.
studenttoremove->courses = remove_course(studenttoremove->courses, coursetoremove);
coursetoremove->students = remove_student(coursetoremove->students, studenttoremove);
}