java不用this方法写一个单链表的学生系统的增删改查
时间: 2024-02-03 14:15:40 浏览: 56
答:好的,我可以为您提供一个简单的单链表学生系统的实现,不使用this方法:
```
class Student {
String name;
int age;
String gender;
Student next;
}
class StudentList {
Student head;
// 增加学生
public void add(String name, int age, String gender) {
Student newStudent = new Student();
newStudent.name = name;
newStudent.age = age;
newStudent.gender = gender;
if (head == null) {
head = newStudent;
} else {
Student current = head;
while (current.next != null) {
current = current.next;
}
current.next = newStudent;
}
}
// 删除学生
public void remove(String name) {
if (head != null) {
if (head.name.equals(name)) {
head = head.next;
} else {
Student current = head;
while (current.next != null && !current.next.name.equals(name)) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next;
}
}
}
}
// 修改学生信息
public void modify(String name, int age, String gender) {
Student current = head;
while (current != null) {
if (current.name.equals(name)) {
current.age = age;
current.gender = gender;
break;
}
current = current.next;
}
}
// 查找学生信息
public Student find(String name) {
Student current = head;
while (current != null) {
if (current.name.equals(name)) {
return current;
}
current = current.next;
}
return null;
}
}
```
以上是一个简单的单链表学生系统的实现,可以进行增加、删除、修改和查找学生信息。具体使用方法可以参考以下示例代码:
```
public class Main {
public static void main(String[] args) {
// 创建学生列表
StudentList students = new StudentList();
// 增加学生
students.add("张三", 20, "男");
students.add("李四", 21, "女");
students.add("王五", 22, "男");
// 查找学生信息
Student student = students.find("李四");
if (student != null) {
System.out.println("姓名:" + student.name);
System.out.println("年龄:" + student.age);
System.out.println("性别:" + student.gender);
}
// 修改学生信息
students.modify("王五", 23, "女");
// 删除学生
students.remove("李四");
}
}
```
希望这个实现对您有帮助,如有疑问请随时提出。
阅读全文