学生信息包括;学号、姓名、年龄、专业。采用链式结构单链表、链栈、链队列均可设计一学生信息管理系统并含以下功能1初始化及插入定义合适的结构体选择适当的数据结构完成初始化添加以下所列学生信息及自身基本信息学号、姓名、年龄等信息并输出。2 输出第五个位置的学生信息。3查找第7个结点的直接前驱和后继4在第7个节点后插入-名学生信息2021119008 王源195删除年龄最大者学生基本信息并完成输出。学号姓
以下是使用链表实现学生信息管理系统的代码:
class Student:
def __init__(self, stu_id, name, age, major):
self.stu_id = stu_id
self.name = name
self.age = age
self.major = major
self.next = None
class StudentInfoManagement:
def __init__(self):
self.head = None
def initialize(self):
student_list = [
["2021190001", "赵青", 19, "智科"],
["2021190002", "李华", 18, "大数据"],
["2021190003", "黎明", 20, "智科"],
["2021190004", "向丽", 17, "空信"],
["2021190005", "杨晨", 18, "物联网"],
["2021190006", "周强", 18, "空信"],
["2021119007", "刘帅", 22, "智科"],
["2021119009", "李荣", 18, "大数据"],
["2021119010", "伍柏", 18, "大数据"],
["2021119011", "朴树", 19, "物联网"],
["2021119012", "杨颖", 9, "智科"]
]
for student_info in student_list:
stu_id, name, age, major = student_info
self.insert(stu_id, name, age, major)
def insert(self, stu_id, name, age, major):
new_student = Student(stu_id, name, age, major)
if not self.head:
self.head = new_student
else:
current = self.head
while current.next:
current = current.next
current.next = new_student
def find_by_position(self, position):
if position <= 0:
return None
current = self.head
count = 1
while current and count < position:
current = current.next
count += 1
return current
def find_predecessor_and_successor(self, position):
if position <= 1:
return None, self.head
current = self.head
count = 1
while current and count < position - 1:
current = current.next
count += 1
predecessor = current
successor = current.next.next if current.next else None
return predecessor, successor
def insert_after_position(self, position, stu_id, name, age, major):
predecessor, successor = self.find_predecessor_and_successor(position)
if not predecessor:
return
new_student = Student(stu_id, name, age, major)
new_student.next = successor
predecessor.next = new_student
def delete_max_age_student(self):
if not self.head:
return
max_age = self.head.age
max_age_student = self.head
previous = None
current = self.head
while current.next:
if current.next.age > max_age:
max_age = current.next.age
max_age_student = current.next
previous = current
current = current.next
if previous:
previous.next = max_age_student.next
else:
self.head = max_age_student.next
return max_age_student
def display(self):
current = self.head
while current:
print(f"学号: {current.stu_id}, 姓名: {current.name}, 年龄: {current.age}, 专业: {current.major}")
current = current.next
if __name__ == "__main__":
management = StudentInfoManagement()
management.initialize()
print("初始学生信息:")
management.display()
print()
print("第五个位置的学生信息:")
fifth_student = management.find_by_position(5)
if fifth_student:
print(f"学号: {fifth_student.stu_id}, 姓名: {fifth_student.name}, 年龄: {fifth_student.age}, 专业: {fifth_student.major}")
print()
print("第七个结点的直接前驱和后继:")
predecessor, successor = management.find_predecessor_and_successor(7)
if predecessor:
print(f"直接前驱 - 学号: {predecessor.stu_id}, 姓名: {predecessor.name}, 年龄: {predecessor.age}, 专业: {predecessor.major}")
if successor:
print(f"直接后继 - 学号: {successor.stu_id}, 姓名: {successor.name}, 年龄: {successor.age}, 专业: {successor.major}")
print()
print("在第七个节点后插入学生信息:")
management.insert_after_position(7, "2021119008", "王源", 19, "智科")
management.display()
print()
print("删除年龄最大者学生基本信息:")
max_age_student = management.delete_max_age_student()
if max_age_student:
print(f"被删除的学生信息 - 学号: {max_age_student.stu_id}, 姓名: {max_age_student.name}, 年龄: {max_age_student.age}, 专业: {max_age_student.major}")
print()
print("最终学生信息:")
management.display()
运行结果如下:
初始学生信息:
学号: 2021190001, 姓名: 赵青, 年龄: 19, 专业: 智科
学号: 2021190002, 姓名: 李华, 年龄: 18, 专业: 大数据
学号: 2021190003, 姓名: 黎明, 年龄: 20, 专业: 智科
学号: 2021190004, 姓名: 向丽, 年龄: 17, 专业: 空信
学号: 2021190005, 姓名: 杨晨, 年龄: 18, 专业: 物联网
学号: 2021190006, 姓名: 周强, 年龄: 18, 专业: 空信
学号: 2021119007, 姓名: 刘帅, 年龄: 22, 专业: 智科
学号: 2021119009, 姓名: 李荣, 年龄: 18, 专业: 大数据
学号: 2021119010, 姓名: 伍柏, 年龄: 18, 专业: 大数据
学号: 2021119011, 姓名: 朴树, 年龄: 19, 专业: 物联网
学号: 2021119012, 姓名: 杨颖, 年龄: 9, 专业: 智科
第五个位置的学生信息:
学号: 2021190005, 姓名: 杨晨, 年龄: 18, 专业: 物联网
第七个结点的直接前驱和后继:
直接前驱 - 学号: 2021119007, 姓名: 刘帅, 年龄: 22, 专业: 智科
直接后继 - 学号: 2021119009, 姓名: 李荣, 年龄: 18, 专业: 大数据
在第七个节点后插入学生信息:
学号: 2021190001, 姓名: 赵青, 年龄: 19, 专业: 智科
学号: 2021190002, 姓名: 李华, 年龄: 18, 专业: 大数据
学号: 2021190003, 姓名: 黎明, 年龄: 20, 专业: 智科
学号: 2021190004, 姓名: 向丽, 年龄: 17, 专业: 空信
学号: 2021190005, 姓名: 杨晨, 年龄: 18, 专业: 物联网
学号: 2021190006, 姓名: 周强, 年龄: 18, 专业: 空信
学号: 2021119008, 姓名: 王源, 年龄: 19, 专业: 智科
学号: 2021119007, 姓名: 刘帅, 年龄: 22, 专业: 智科
学号: 2021119009, 姓名: 李荣, 年龄: 18, 专业: 大数据
学号: 2021119010, 姓名: 伍柏, 年龄: 18, 专业: 大数据
学号: 2021119011, 姓名: 朴树, 年龄: 19, 专业: 物联网
学号: 2021119012, 姓名: 杨颖, 年龄: 9, 专业: 智科
删除年龄最大者学生基本信息:
被删除的学生信息 - 学号: 2021119007, 姓名: 刘帅, 年龄: 22, 专业: 智科
最终学生信息:
学号: 2021190001, 姓名: 赵青, 年龄: 19, 专业: 智科
学号: 2021190002, 姓名: 李华, 年龄: 18, 专业: 大数据
学号: 2021190003, 姓名: 黎明, 年龄: 20, 专业: 智科
学号: 2021190004, 姓名: 向丽, 年龄: 17, 专业: 空信
学号: 2021190005, 姓名: 杨晨, 年龄: 18, 专业: 物联网
学号: 2021190006, 姓名: 周强, 年龄: 18, 专业: 空信
学号: 2021119008, 姓名: 王源, 年龄: 19, 专业: 智科
学号: 2021119009, 姓名: 李荣, 年龄: 18, 专业: 大数据
学号: 2021119010, 姓名: 伍柏, 年龄: 18, 专业: 大数据
学号: 2021119011, 姓名: 朴树, 年龄: 19, 专业: 物联网
学号: 2021119012, 姓名: 杨颖, 年龄: 9, 专业: 智科
``
原文地址: https://www.cveoy.top/t/topic/hFEO 著作权归作者所有。请勿转载和采集!