Java如何在本地创建链表

引言

链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。在Java中,我们可以使用自定义的类来表示链表,并通过设置节点之间的关联来实现链表的创建和操作。

本文将介绍如何在本地创建链表,并解决一个实际问题:在一个学生管理系统中,如何使用链表来存储和管理学生信息。

问题描述

假设我们需要实现一个学生管理系统。系统需要能够添加、删除和查找学生,以及展示所有学生的信息。我们可以使用链表来存储学生信息,每个节点表示一个学生。

设计思路

我们可以使用一个自定义的Student类来表示学生信息,包含学生的姓名、年龄和学号等属性。然后,我们可以创建一个Node类作为链表的节点,每个节点包含一个学生对象和指向下一个节点的引用。

接下来,我们需要创建一个LinkedList类来实现链表的操作。这个类包含一个指向链表头部的引用,并提供方法来添加、删除和查找学生。

类图

classDiagram
    class Student {
        -name: String
        -age: int
        -id: String
        +Student(name: String, age: int, id: String)
        +getName(): String
        +getAge(): int
        +getId(): String
    }

    class Node {
        -student: Student
        -next: Node
        +Node(student: Student)
        +getStudent(): Student
        +getNext(): Node
        +setNext(next: Node): void
    }

    class LinkedList {
        -head: Node
        +LinkedList()
        +addStudent(student: Student): void
        +removeStudent(id: String): void
        +findStudent(id: String): Student
        +displayStudents(): void
    }

    Student --> Node
    Node --> LinkedList

实现代码

下面是完整的Java代码实现。

Student类

public class Student {
    private String name;
    private int age;
    private String id;

    public Student(String name, int age, String id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getId() {
        return id;
    }
}

Node类

public class Node {
    private Student student;
    private Node next;

    public Node(Student student) {
        this.student = student;
        this.next = null;
    }

    public Student getStudent() {
        return student;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

LinkedList类

public class LinkedList {
    private Node head;

    public LinkedList() {
        head = null;
    }

    public void addStudent(Student student) {
        Node newNode = new Node(student);

        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            current.setNext(newNode);
        }
    }

    public void removeStudent(String id) {
        if (head == null) {
            return;
        }

        if (head.getStudent().getId().equals(id)) {
            head = head.getNext();
            return;
        }

        Node current = head;
        while (current.getNext() != null) {
            if (current.getNext().getStudent().getId().equals(id)) {
                current.setNext(current.getNext().getNext());
                return;
            }
            current = current.getNext();
        }
    }

    public Student findStudent(String id) {
        Node current = head;
        while (current != null) {
            if (current.getStudent().getId().equals(id)) {
                return current.getStudent();
            }
            current = current.getNext();
        }
        return null;
    }

    public void displayStudents() {
        Node current = head;
        while (current != null) {
            System.out.println("Name: " + current.getStudent().getName());
            System.out.println("Age: " + current.getStudent().getAge());
            System.out.println("ID: " + current.getStudent().getId());
            System.out.println("------------------------");
            current = current.getNext();
        }
    }
}

使用示例

下面是一个使用链表管理学生信息的示例代码。

public class StudentManagementSystem {
    public static void main(String[] args) {
        LinkedList studentList = new LinkedList();