用python的链表写一份学生管理系统,学生信息包括:学号,姓名,总分,帮我列出代码
时间: 2024-03-13 09:21:50 浏览: 65
学生信息管理系统, 实现学生信息包括:学号、姓名、性别、年龄、班级等信息
5星 · 资源好评率100%
以下是用Python链表实现的学生管理系统代码:
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
self.next = None
class StudentManager:
def __init__(self):
self.head = None
def add_student(self, id, name, score):
new_student = Student(id, name, score)
if self.head is None:
self.head = new_student
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_student
def search_student(self, id):
current = self.head
while current is not None:
if current.id == id:
return current
current = current.next
return None
def delete_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_students(self):
current = self.head
while current is not None:
print("ID: {}, Name: {}, Score: {}".format(current.id, current.name, current.score))
current = current.next
```
以上代码包含了添加学生、查找学生、删除学生和打印所有学生的功能。使用时,可以创建一个`StudentManager`对象,然后调用其方法实现对学生信息的管理。例如:
```python
manager = StudentManager()
manager.add_student(1, "Tom", 80)
manager.add_student(2, "Jerry", 90)
manager.add_student(3, "Alice", 85)
manager.print_students()
student = manager.search_student(2)
if student is not None:
print("Found student: ID: {}, Name: {}, Score: {}".format(student.id, student.name, student.score))
else:
print("Student not found")
manager.delete_student(3)
manager.print_students()
```
输出结果为:
```
ID: 1, Name: Tom, Score: 80
ID: 2, Name: Jerry, Score: 90
ID: 3, Name: Alice, Score: 85
Found student: ID: 2, Name: Jerry, Score: 90
ID: 1, Name: Tom, Score: 80
ID: 2, Name: Jerry, Score: 90
```
阅读全文