JAVA:常用的链表指南

请关注微信公众号:拾荒的小海螺
博客地址:http://lsk-ww.cn/

1、简述

链表是一种常见的数据结构,它通过一系列节点(Node)来表示一个序列。每个节点包含数据和指向下一个节点的引用。链表的种类有很多,包括单向链表、双端链表、有序链表和双向链表。本文将介绍这些链表的基本概念和 Java 实现。
在这里插入图片描述

2、单向链表(Singly Linked List)

单向链表是一种最基本的链表结构,每个节点只包含一个指向下一个节点的引用。链表的第一个节点称为头节点(head),最后一个节点的下一个引用指向 null,以下是单向链表的实例:

class SinglyLinkedList {
    private Node head;

    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }

    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    public static void main(String[] args) {
        SinglyLinkedList list = new SinglyLinkedList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.printList(); // 输出: 1 2 3
    }
}

3、双端链表(Doubly Ended Linked List)

双端链表是一个扩展的单向链表,它在链表中维护对最后一个节点(尾节点)的引用,以便于在链表末尾进行快速插入操作,以下是双端链表的实例:

class DoublyEndedLinkedList {
    private Node head;
    private Node tail;

    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    public void addLast(int data) {
        Node newNode = new Node(data);
        if (tail == null) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
    }

    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    public static void main(String[] args) {
        DoublyEndedLinkedList list = new DoublyEndedLinkedList();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.printList(); // 输出: 1 2 3
    }
}

4、有序链表(Sorted Linked List)

有序链表是一种链表,其中元素按升序排列。每次插入新元素时,链表会自动将其插入到正确的位置以保持顺序,以下是有序链表的实例:

class SortedLinkedList {
    private Node head;

    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null || head.data >= newNode.data) {
            newNode.next = head;
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null && current.next.data < newNode.data) {
                current = current.next;
            }
            newNode.next = current.next;
            current.next = newNode;
        }
    }

    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    public static void main(String[] args) {
        SortedLinkedList list = new SortedLinkedList();
        list.add(3);
        list.add(1);
        list.add(2);
        list.printList(); // 输出: 1 2 3
    }
}

5、双向链表(Doubly Linked List)

双向链表是一种链表,其中每个节点包含指向前一个节点和后一个节点的引用。这使得在链表中可以向前和向后遍历,以下是双向链表的实例:

class DoublyLinkedList {
    private Node head;
    private Node tail;

    private static class Node {
        int data;
        Node prev;
        Node next;

        Node(int data) {
            this.data = data;
            this.prev = null;
            this.next = null;
        }
    }

    public void add(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
    }

    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }

    public void printListReverse() {
        Node current = tail;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.prev;
        }
    }

    public static void main(String[] args) {
        DoublyLinkedList list = new DoublyLinkedList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.printList(); // 输出: 1 2 3
        System.out.println();
        list.printListReverse(); // 输出: 3 2 1
    }
}

6、总结

本文介绍了四种常见的链表及其 Java 实现:单向链表、双端链表、有序链表和双向链表。每种链表都有其独特的特点和应用场景。单向链表结构简单,适合于基础数据存储;双端链表适合于需要快速在末尾插入的场景;有序链表适用于需要保持顺序的数据集合;双向链表则适合于需要双向遍历的复杂应用。通过这些示例,希望您能更好地理解链表结构及其在 Java 中的实现。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拾荒的小海螺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值