顺序表学生学号,姓名,成绩且有定义,插入,删除,排序操作
时间: 2024-06-13 12:08:38 浏览: 95
数据结构:定义一个包含学生信息(学号、姓名、成绩)的顺序表_数据结构
5星 · 资源好评率100%
以下是顺序表学生学号、姓名、成绩的定义、插入、删除、排序操作的Python代码:
1.定义顺序表
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
class StudentList:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def sort_students(self):
self.students.sort(key=lambda x: x.score, reverse=True)
```
2.插入学生信息
```python
student_list = StudentList()
student_list.add_student(Student(1, 'Tom', 90))
student_list.add_student(Student(2, 'Jerry', 80))
```
3.删除学生信息
```python
student_list.remove_student(1)
```
4.排序学生信息
```python
student_list.sort_students()
```
阅读全文