在java中如何实现实现小型学生信息管理程序(不用实现界面),功能包括基本的添加学生、删除、修改和查询学生信息等操作,要求用链表实现:用户可选择五个操作之一:添加学生、修改学生、删除学生、查询学生、显示学生链表的当前状态和退出程序,测试上述所有功能。
时间: 2024-03-15 16:43:51 浏览: 23
好的,我来回答你的问题。首先,你需要定义一个学生类,包含学生的基本信息,如姓名、性别、年龄、学号等。然后,你需要定义一个链表类,用来存储学生信息,并实现添加、删除、修改和查询等操作。
以下是示例代码:
```java
// 学生类
class Student {
private String name;
private String gender;
private int age;
private int id;
public Student(String name, String gender, int age, int id) {
this.name = name;
this.gender = gender;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public int getId() {
return id;
}
}
// 链表节点类
class Node {
private Student student;
private Node next;
public Node(Student student) {
this.student = student;
}
public Student getStudent() {
return student;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
}
}
// 链表类
class LinkedList {
private Node head;
public LinkedList() {
head = new Node(null);
}
// 添加学生
public void add(Student student) {
Node newNode = new Node(student);
Node p = head;
while (p.getNext() != null) {
p = p.getNext();
}
p.setNext(newNode);
}
// 删除学生
public void remove(int id) {
Node p = head.getNext();
Node q = head;
while (p != null) {
if (p.getStudent().getId() == id) {
q.setNext(p.getNext());
return;
}
q = p;
p = p.getNext();
}
}
// 修改学生
public void update(Student student) {
Node p = head.getNext();
while (p != null) {
if (p.getStudent().getId() == student.getId()) {
p.getStudent().setName(student.getName());
p.getStudent().setGender(student.getGender());
p.getStudent().setAge(student.getAge());
return;
}
p = p.getNext();
}
}
// 查询学生
public Student find(int id) {
Node p = head.getNext();
while (p != null) {
if (p.getStudent().getId() == id) {
return p.getStudent();
}
p = p.getNext();
}
return null;
}
// 显示学生链表的当前状态
public void display() {
Node p = head.getNext();
while (p != null) {
System.out.println("姓名:" + p.getStudent().getName() + ",性别:" + p.getStudent().getGender() + ",年龄:" + p.getStudent().getAge() + ",学号:" + p.getStudent().getId());
p = p.getNext();
}
}
}
// 主程序
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(new Student("张三", "男", 20, 1));
list.add(new Student("李四", "女", 21, 2));
list.add(new Student("王五", "男", 22, 3));
list.display();
list.remove(2);
list.display();
list.update(new Student("张三", "男", 22, 1));
list.display();
Student student = list.find(3);
if (student != null) {
System.out.println("姓名:" + student.getName() + ",性别:" + student.getGender() + ",年龄:" + student.getAge() + ",学号:" + student.getId());
}
}
}
```
这是一个简单的学生信息管理程序,使用链表实现了基本的添加、删除、修改和查询操作。你可以根据自己的需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)