本程序是通过链表实现学生个人信息(包括学号、姓名和分数)的载入(从磁盘 文件中读入)、显示和删除等功能。其中: (1) 头文件head.h包含有stdio.h和其他系统头文件,以及结构体的定义等内容。 (2) 菜单函数mainMenu()和主函数main()已经完整给出,无需修改。 (3) 请编写载入函数loadInfo()、显示函数displayInfo()和删除函数delInfo(), 其形参和返回值类型见程序中的函数声明。 (4) 载入函数loadInfo()实现将给定文件C:\KS\A_6_1.txt中的学生信息读入内存 (链表)中。 (5) 显示函数displayInfo()实现完整显示链表中学生信息。 (6) 删除函数delInfo()将内存链表中指定学号的信息删除。 (7) 设计一个链表结点查找函数,供delInfo()函数在删除前查找定位使用,函数 名称、参数表与返回类型自主设计,要求不能包含任何输入输出功能。
时间: 2024-03-31 12:37:24 浏览: 17
好的,我明白了。这是一个关于链表的程序,其中包含载入、显示和删除功能。你的第一个问题是:
程序中提到了哪些函数需要实现?它们的功能是什么?
答:程序中需要实现载入函数loadInfo()、显示函数displayInfo()和删除函数delInfo()。它们的功能分别是:
- loadInfo():将给定文件中的学生信息读入内存中的链表中。
- displayInfo():完整显示链表中的学生信息。
- delInfo():删除内存链表中指定学号的信息。
此外,还需要设计一个链表结点查找函数,供delInfo()函数在删除前查找定位使用。该函数名称、参数表与返回类型自主设计,要求不能包含任何输入输出功能。
相关问题
5个学生信息建立动态链表,每个学生包括学号、姓名和分数 3种信息(要求这些信息能从文件中导入),输入一个学号值删除列表中该学号的学生节点
可以使用以下代码实现:
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
self.next = None
class StudentList:
def __init__(self):
self.head = None
def add_student(self, id, name, score):
student = Student(id, name, score)
if self.head is None:
self.head = student
else:
current = self.head
while current.next is not None:
current = current.next
current.next = student
def remove_student(self, id):
if self.head is None:
return
if self.head.id == id:
self.head = self.head.next
return
current = self.head
while current.next is not None:
if current.next.id == id:
current.next = current.next.next
return
current = current.next
def print_list(self):
current = self.head
while current is not None:
print("ID: {}, Name: {}, Score: {}".format(current.id, current.name, current.score))
current = current.next
def load_from_file(self, filename):
with open(filename, "r") as f:
for line in f:
id, name, score = line.strip().split(",")
self.add_student(id, name, score)
def save_to_file(self, filename):
with open(filename, "w") as f:
current = self.head
while current is not None:
f.write("{},{},{}\n".format(current.id, current.name, current.score))
current = current.next
# Example usage
students = StudentList()
students.load_from_file("students.txt")
students.remove_student("123")
students.print_list()
students.save_to_file("students.txt")
```
其中,`Student` 类表示一个学生,包括学号、姓名和分数三个属性,以及一个指向下一个学生的指针。`StudentList` 类表示一个学生列表,包括一个头指针,以及添加、删除、打印和从文件中导入/导出学生信息的方法。在 `load_from_file` 方法中,我们从文件中读取每一行学生信息,然后调用 `add_student` 方法将其添加到链表中。在 `remove_student` 方法中,我们遍历链表,找到要删除的学生节点,然后将其从链表中移除。最后,我们可以调用 `print_list` 方法打印链表中所有学生的信息,或者调用 `save_to_file` 方法将链表中的学生信息保存到文件中。
为5个学生信息建立动态链表,每个学生包括学号、姓名和分数 3种信息,输入一个学号值删除列表中该学号的学生节点。
以下是Python代码实现:
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add_student(self, student):
if self.head is None:
self.head = student
else:
current = self.head
while current.next is not None:
current = current.next
current.next = student
def remove_student(self, id):
if self.head is None:
return
if self.head.id == id:
self.head = self.head.next
return
current = self.head
while current.next is not None:
if current.next.id == id:
current.next = current.next.next
return
current = current.next
def print_list(self):
current = self.head
while current is not None:
print(f"学号:{current.id},姓名:{current.name},分数:{current.score}")
current = current.next
# 创建链表并添加学生
linked_list = LinkedList()
student1 = Student(1, "张三", 80)
student2 = Student(2, "李四", 90)
student3 = Student(3, "王五", 85)
student4 = Student(4, "赵六", 95)
student5 = Student(5, "钱七", 70)
linked_list.add_student(student1)
linked_list.add_student(student2)
linked_list.add_student(student3)
linked_list.add_student(student4)
linked_list.add_student(student5)
# 打印链表
print("删除前的链表:")
linked_list.print_list()
# 删除学号为3的学生
linked_list.remove_student(3)
# 打印链表
print("删除后的链表:")
linked_list.print_list()
```
输出结果:
```
删除前的链表:
学号:1,姓名:张三,分数:80
学号:2,姓名:李四,分数:90
学号:3,姓名:王五,分数:85
学号:4,姓名:赵六,分数:95
学号:5,姓名:钱七,分数:70
删除后的链表:
学号:1,姓名:张三,分数:80
学号:2,姓名:李四,分数:90
学号:4,姓名:赵六,分数:95
学号:5,姓名:钱七,分数:70
```