欢迎访问宙启技术站
智能推送

如何使用JAVA函数实现链表数据结构

发布时间:2023-05-19 18:26:06

链表是一种常用的数据结构,它是由一系列结点组成的,每个结点包含着数据和指向下一个结点的引用。链表可以按顺序插入、删除和查找元素,因此在很多算法和数据处理中都有广泛的应用。在JAVA中,我们可以通过自定义类和方法来实现链表数据结构。

1. 建立结点类

我们可以通过一个Node类来表示链表的结点。Node类至少包含以下两个成员变量:数据值(value)和指向下一个结点的引用(next)。

public class Node {

    int value;

    Node next;

    public Node(int value, Node next) {

        this.value = value;

        this.next = next;

    }

    public int getValue() {

        return this.value;

    }

    public void setValue(int value) {

        this.value = value;

    }

    public Node getNext() {

        return this.next;

    }

    public void setNext(Node next) {

        this.next = next;

    }

}

2. 建立链表类

在建立好结点类后,我们需要建立一个链表类,用于存储和操作链表。链表类至少包含以下成员变量:链表的头结点(head)和链表的长度(length)。

public class LinkedList {

    Node head;

    int length;

    // 构造方法:创建一个空链表

    public LinkedList() {

        this.head = null;

        this.length = 0;

    }

    // 添加结点

    public void add(int value) {

        Node newNode = new Node(value, null);

        if (head == null) {

            head = newNode;

        } else {

            Node current = head;

            while (current.getNext() != null) {

                current = current.getNext();

            }

            current.setNext(newNode);

        }

        length++;

    }

    // 删除结点

    public void remove(int index) {

        if (index < 0 || index >= length) {

            System.out.println("Index out of range.");

        } else {

            if (index == 0) {

                head = head.getNext();

            } else {

                Node current = head;

                for (int i = 0; i < index - 1; i++) {

                    current = current.getNext();

                }

                current.setNext(current.getNext().getNext());

            }

            length--;

        }

    }

    // 查询结点

    public Node get(int index) {

        if (index < 0 || index >= length) {

            System.out.println("Index out of range.");

            return null;

        } else {

            Node current = head;

            for (int i = 0; i < index; i++) {

                current = current.getNext();

            }

            return current;

        }

    }

    // 返回链表长度

    public int size() {

        return length;

    }

    // 遍历链表

    public void display() {

        Node current = head;

        while (current != null) {

            System.out.print(current.getValue() + " ");

            current = current.getNext();

        }

        System.out.println();

    }

}

3. 测试链表类

为了验证链表类是否可用,我们需要编写一些简单的测试代码,用于添加、删除、查询和显示节点。

public class TestLinkedList {

    public static void main(String[] args) {

        LinkedList list = new LinkedList();

        // 添加结点

        list.add(1);

        list.add(2);

        list.add(3);

        list.add(4);

        System.out.print("链表列表: ");

        list.display();

        // 删除结点

        list.remove(0);

        System.out.print("删除 个结点: ");

        list.display();

        // 查询结点

        Node node = list.get(1);

        System.out.println("查询第二个节点的值为: " + node.getValue());

        // 遍历链表

        System.out.print("遍历链表: ");

        list.display();

        // 返回链表长度

        System.out.println("链表长度为: " + list.size());

    }

}

运行测试代码后,可以得到以下输出结果:

链表列表: 1 2 3 4 

删除 个结点: 2 3 4 

查询第二个节点的值为: 3

遍历链表: 2 3 4 

链表长度为: 3

通过以上测试练习,我们成功地实现了链表数据结构,可以通过添加、删除、查询和遍历结点来操作链表。对于更复杂的应用场景,我们可以上面的代码进行继续扩展,例如增加对链表中间插入结点的支持、在链表中查询某个值的结点等等。