Java--双向链表

双向链表也是链表的一种,特殊性在于:它对比单链表多了一个指向前驱(prev)的引用。
注意:head----指向链表的第一个结点
last------指向链表最后一个结点
size------当前链表结点个数 在这里插入图片描述

class Node {
    int val;
    Node next = null;   // 指向后继结点,最后一个结点的 next == null
    Node prev = null;   // 指向前驱结点,第一个结点的 prev == null

    Node(int val) {
        this.val = val;
    }
}
public class MyLinkedList {
    private Node head = null;
    private Node last = null;
    private int size = 0;

    public void pushFront(int val) {//头插创建
        Node node = new Node(val); //定义一个结点
        node.next = head;
        if (head != null) {
            head.prev = node;
        } else {
            last = node;
        }
        head = node;

        size++;
    }

    public void popFront() { //头删
        if (size <= 0) {
            System.out.println("无法对空链表做删除");
            return;
        }

        head = head.next;
        if (head != null) {
            head.prev = null;
        } else {
            last = null;
        }
        size--;
    }

    void pushBack(int val) { //尾插创建
        Node node = new Node(val);
        if (last == null) {
            head = node;
        } else {
            last.next = node;
        }
        node.prev = last;
        last = node;

        size++;
    }

    void popBack() { //尾删
        if (size <= 0) {
            System.out.println("无法对空链表做删除");
            return;
        }

        if (last == head) {
            head = last = null;
        } else {
            last.prev.next = null;
            last = last.prev;
        }

        size--;
    }

    public void add(int index, int val) { //插入
        if (index < 0 || index > size) {
            System.out.println("下标错误");
            return;
        }
        if (index == 0) {
            pushFront(val);
        } else if (index == size) {
            pushBack(val);
        } else {
            Node pos = getNode(index);
            Node node = new Node(val);
            node.prev = pos.prev;
            node.next = pos;
            node.prev.next = node;
            node.next.prev = node;

            size++;
        }
    }

    public void remove(int index) { //删除
        if (size <= 0) {
            System.out.println("无法对空链表做删除");
            return;
        }

        if (index < 0 || index >= size) {
            System.out.println("下标错误");
            return;
        }

        if (index == 0) {
            popFront();
        } else if (index == size - 1) {
            popBack();
        } else {
            Node pos = getNode(index);
            pos.prev.next = pos.next;
            pos.next.prev = pos.prev;
            size--;
        }
    }

    // 代码的复用性
    private Node getNode(int index) {
        // 不需要校验 index 的合法性
        // 因为使用者 add 和 remove 已经做过类似工作了
        int backwardIndex = size - index - 1;
        Node pos;
        if (index <= backwardIndex) {
            pos = head;
            for (int i = 0; i < index; i++) {
                pos = pos.next;
            }
        } else {
            pos = last;
            for (int i = 0; i < backwardIndex; i++) {
                pos = pos.prev;
            }
        }

        return pos;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void reset() {
        head = last = null;
        size = 0;
    }



    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.pushFront(1);
        list.pushFront(2);
        list.pushFront(3);
        System.out.println("3 2 1");
        list.popFront();
        list.popFront();
        list.popFront();
        System.out.println("");
        list.pushBack(10);
        list.pushBack(20);
        list.pushBack(30);
        System.out.println("10 20 30");
        list.popBack();
        list.popBack();
        list.popBack();
        System.out.println("");
        list.pushBack(1);
        list.pushBack(2);
        list.pushBack(3);
        list.pushBack(4);
        list.pushBack(5);
        System.out.println("1 2 3 4 5");
        list.add(1, 10);
        System.out.println("1 10 2 3 4 5");
        list.add(5, 20);
        System.out.println("1 10 2 3 4 20 5");
        list.remove(1);
        System.out.println("1 2 3 4 20 5");
        list.remove(4);
        System.out.println("1 2 3 4 5");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的环形双向链表是一种特殊的数据结构,它的每个节点都包含了前驱和后继指针,而且尾节点的后继指向头节点,头节点的前驱指向尾节点。这种设计允许在链表中进行双向遍历,并且可以很方便地在任意位置插入或删除节点。 环形双向链表Java中的应用非常广泛,尤其在需要频繁插入或删除节点的情况下,其效率较高。双向链表可以通过直接获取到当前节点的前一个节点,实现快速插入和删除操作,时间复杂度为O(1)。而当需要循环遍历链表的时候,环形双向链表可以很方便地实现循环执行的功能,无需额外的判断条件。 Java中环形双向链表最常见的应用是LinkedList类的实现。LinkedList是Java集合框架中的一个双向链表实现,通过将首尾节点相连形成环形双向链表,可以实现高效的插入、删除和遍历操作。 总之,Java环形双向链表是一种灵活高效的数据结构,适用于需要频繁插入、删除和循环遍历的场景。它在Java集合框架中的LinkedList中得到了广泛的应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [java实现双向循环链表(循环双链表)](https://blog.csdn.net/m0_46897923/article/details/115905703)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值