6-1 建立学生信息链表
时间: 2024-05-09 14:19:40 浏览: 89
抱歉,作为AI语言模型,我无法直接进行编程操作。但是,我可以为你提供一个基本的学生信息链表的结构,你可以根据这个结构进行编程实现。
学生信息链表结构:
```python
class Student:
def __init__(self, name, age, gender, id):
self.name = name
self.age = age
self.gender = gender
self.id = id
self.next = None
class StudentList:
def __init__(self):
self.head = None
def add_student(self, student):
if not self.head:
self.head = student
else:
current = self.head
while current.next:
current = current.next
current.next = student
def remove_student(self, id):
current = self.head
previous = None
while current and current.id != id:
previous = current
current = current.next
if not current:
return False
if not previous:
self.head = current.next
else:
previous.next = current.next
return True
def find_student(self, id):
current = self.head
while current and current.id != id:
current = current.next
if not current:
return None
return current
def print_list(self):
current = self.head
while current:
print("Name:", current.name)
print("Age:", current.age)
print("Gender:", current.gender)
print("ID:", current.id)
current = current.next
```
在这个结构中,我们定义了两个类 `Student` 和 `StudentList`。`Student` 类表示一个学生的信息,包括姓名、年龄、性别和学号;`StudentList` 类表示一个学生信息的链表,包括链表的头节点 `head` 和一些基本操作方法,如添加学生、删除学生、查找学生和打印链表。你可以根据这个结构进行编程实现,并根据自己的需求进行调整和优化。
阅读全文