我有一个extension MFMailComposeViewController {
public override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
}
public override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
public override func childViewControllerForStatusBarStyle() -> UIViewController? {
return nil
}
}
extension MFMessageComposeViewController {
public override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
}
public override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
public override func childViewControllerForStatusBarStyle() -> UIViewController? {
return nil
}
}
,其中包含School struct
个链接列表,每个Student struct
都包含Student struct
个链接列表。关于我如何能够释放两个链表,我有点困惑。我特别不确定如何释放Courses struct
链表,因为它在另一个链表中。
Courses
如果有人能告诉我一个关于我如何能够释放两个很棒的链接列表的示例!
答案 0 :(得分:3)
你应该考虑所有权:当你释放一个项目时,你必须释放它拥有的所有东西,这就是他指向的所有东西都不是拥有的通过别的东西。
沿着这些方向,每个列表项拥有下一个项目,每个School
拥有其Student
个列表,每个{{1拥有Student
的列表。
您的类型定义似乎不正确,因为您对类型和变量使用相同的标识符。你应该这样重写它们:
Courses
释放一切的功能:
typedef struct Course Course;
typedef struct Student Student;
typedef struct School School;
struct Course {
char *courseName;
int creditValue;
Course *next;
};
struct Student {
char *studentName;
int studentAge;
Course *courseList; //First course (node)
Student *next;
};
struct School {
char *schoolName;
int schoolAge;
Student *studentList; //First course (node)
};
注意不同级别之间的相似性。您还可以将此功能拆分为单独的void freeSchool(School *school) {
Student *student = school->studentList;
while (student) {
Course *course = student->courseList;
while (course) {
free(course->courseName); // if it was allocated
Course *nextCourse = course->next;
free(course);
course = nextCourse;
}
free(student->studentName); // if it was allocated
Student *nextStudent = student->next;
free(student);
student = nextStudent;
}
free(school->schoolName); // if it was allocated
free(school);
}
,freeSchool
和freeStudent
函数,您可以使用这些函数来处理程序中的单个对象删除。
处理元素删除的一种优雅方法是freeCourse
函数返回下一个元素并释放节点:
freeXXX