定义一个学生类和学生类对象的链表类 #补充代码,打印本同学的全部信息 #补充代码,能将文件中的信息,读入到self.Tset中 #补充代码,能将self.Tset信息,写入到文件filname中 #补充代码,对TSet中的学生的身高进行升序排序,注意排序,可通过 在有序链表 #补充代码,输出TSet中身高大于y的学生个数 #补充代码,能将TSet中性别为x的学生都删除 #补充代码,删除TSet中学号为sid的学生 #补充代码,添加身高为hei的学生到TSet中,要求添加学生后,学生表中的身高保
时间: 2023-11-28 10:47:41 浏览: 71
下来是补充代码:
```python
class Student:
def __init__(self, sname, sid, sgender, sage, sheight):
self.sname = sname
self.sid = sid
self.sgender = sgender
self.sage = sage
self.sheight = sheight
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# 补充代码,打印本同学的全部信息
def print_student_info(self):
current = self.head
while current:
if current.data.sname == '张三':
print(f'姓名:{current.data.sname},学号:{current.data.sid},性别:{current.data.sgender},年龄:{current.data.sage},身高:{current.data.sheight}')
current = current.next
# 补充代码,能将文件中的信息,读入到self.Tset中
def read_file(self, filename):
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
sname, sid, sgender, sage, sheight = line.split(',')
student = Student(sname, sid, sgender, int(sage), float(sheight))
self.add_student(student)
# 补充代码,能将self.Tset信息,写入到文件filname中
def write_file(self, filename):
with open(filename, 'w') as f:
current = self.head
while current:
f.write(f'{current.data.sname},{current.data.sid},{current.data.sgender},{current.data.sage},{current.data.sheight}\n')
current = current.next
# 补充代码,对TSet中的学生的身高进行升序排序,注意排序,可通过在有序链表
def sort_by_height(self):
new_list = LinkedList()
current = self.head
while current:
next_node = current.next
new_list.insert_sorted(current)
current = next_node
self.head = new_list.head
def insert_sorted(self, node):
if not self.head:
self.head = node
elif self.head.data.sheight > node.data.sheight:
node.next = self.head
self.head = node
else:
current = self.head
prev = None
while current and current.data.sheight < node.data.sheight:
prev = current
current = current.next
prev.next = node
node.next = current
# 补充代码,输出TSet中身高大于y的学生个数
def count_by_height(self, y):
count = 0
current = self.head
while current:
if current.data.sheight > y:
count += 1
current = current.next
print(f'身高大于{y}的学生个数为:{count}')
# 补充代码,能将TSet中性别为x的学生都删除
def delete_by_gender(self, x):
current = self.head
prev = None
while current:
if current.data.sgender == x:
if prev:
prev.next = current.next
else:
self.head = current.next
else:
prev = current
current = current.next
# 补充代码,删除TSet中学号为sid的学生
def delete_by_id(self, sid):
current = self.head
prev = None
while current:
if current.data.sid == sid:
if prev:
prev.next = current.next
else:
self.head = current.next
break
else:
prev = current
current = current.next
# 补充代码,添加身高为hei的学生到TSet中,要求添加学生后,学生表中的身高保持升序
def add_student(self, student):
node = Node(student)
if not self.head:
self.head = node
elif self.head.data.sheight > student.sheight:
node.next = self.head
self.head = node
else:
current = self.head
prev = None
while current and current.data.sheight < student.sheight:
prev = current
current = current.next
prev.next = node
node.next = current
```
这是一个完整的链表类,可以通过调用相应方法进行链表的操作,例如:
```python
# 创建 TSet 对象
TSet = LinkedList()
# 读入文件信息
TSet.read_file('students.txt')
# 打印本同学的全部信息
TSet.print_student_info()
# 对 TSet 中的学生的身高进行升序排序
TSet.sort_by_height()
# 输出 TSet 中身高大于 y 的学生个数
TSet.count_by_height(1.8)
# 删除 TSet 中性别为 x 的学生
TSet.delete_by_gender('男')
# 删除 TSet 中学号为 sid 的学生
TSet.delete_by_id('0001')
# 添加身高为 hei 的学生到 TSet 中
TSet.add_student(Student('李四', '0004', '女', 20, 1.6))
# 将 TSet 信息写入到文件中
TSet.write_file('students.txt')
```
阅读全文