Java实现双链表的示例代码

 更新时间:2022年09月26日 09:10:37   作者:熬夜磕代码丶  
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。本文将用Java语言实现双链表,需要的可以参考一下

一、双向链表是什么

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。

LinkedList底层就是一个双向链表,我们来实现一个双向链表。

这里多一个尾指针,方便我们对尾插操作从O(n)降到O(1).每个结点多了前驱结点,方便我们对链表进行操作。

二、具体方法实现

定义结点

class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

下标访问异常

public class IndexWrongException extends RuntimeException{
    public IndexWrongException() {
    }

    public IndexWrongException(String message) {
        super(message);
    }
}

获取链表长度

class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

打印链表

class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

清空链表

public void clear(){
        if(this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }

头插法

public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
    }

尾插法

public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        tail = node;
    }

指定位置插入

public void addIndex(int index,int data) throws IndexWrongException{
        if(index < 0 || index > size()) {
            throw new IndexWrongException("输入下标不合法");
        }
        ListNode node = new ListNode(data);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = this.head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

查找元素

public boolean contains(int key){
        if(head == null) {
            return false;
        }
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

删除第一次出现的关键字

public void remove(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                        }
                    }
                return;
                }
            cur = cur.next;
            }
        }

删除所有值为key的节点

public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                    }
                }
            }
            cur = cur.next;
        }
    }

三、完整代码

public class LinkedList {
    static class ListNode {
        int value;
        ListNode next;
        ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }
    ListNode head;
    ListNode tail;
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            this.head = node;
            this.tail = node;
            return;
        }
        tail.next = node;
        node.prev = tail;
        tail = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws IndexWrongException{
        if(index < 0 || index > size()) {
            throw new IndexWrongException("输入下标不合法");
        }
        ListNode node = new ListNode(data);
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = this.head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head == null) {
            return false;
        }
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                        }
                    }
                return;
                }
            cur = cur.next;
            }
        }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != head) {
            if(cur.value == key) {
                if(cur == head) {
                    head = head.next;
                    if(head.next != null) {
                        head.prev = null;
                    }else {
                        tail = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                    }else {
                        tail = cur.prev;
                        tail.next = null;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear(){
        if(this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
    }
}

到此这篇关于Java实现双链表的示例代码的文章就介绍到这了,更多相关Java双链表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JAVA JDK8 List获取属性列表

    JAVA JDK8 List获取属性列表

    今天小编就为大家分享一篇关于JAVA JDK8 List获取属性列表,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 完美解决Java中的线程安全问题

    完美解决Java中的线程安全问题

    下面小编就为大家带来一篇完美解决Java中的线程安全问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Java算法之重新排列数组例题

    Java算法之重新排列数组例题

    这篇文章主要介绍了Java算法之重新排列数组例题,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感兴趣的小伙伴可以参考一下
    2022-08-08
  • Lombok基本注解之@SneakyThrows的作用

    Lombok基本注解之@SneakyThrows的作用

    @SneakyThrows注解是由lombok为咱们封装的,它能够为咱们的代码生成一个try...catch块,并把异常向上抛出来,下面这篇文章主要给大家介绍了关于Lombok基本注解之@SneakyThrows作用的相关资料,需要的朋友可以参考下
    2022-01-01
  • Java生成的随机数靠谱吗?多少次会重复?

    Java生成的随机数靠谱吗?多少次会重复?

    今天给大家带来的是关于Java的相关知识,文章围绕着Java生成的随机数靠不靠谱展开,文中有非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • MyBatis-Plus实现分页的方法使用详解

    MyBatis-Plus实现分页的方法使用详解

    这篇文章主要为大家介绍了MyBatis-Plus的分页的方法使用,包括:不传参数时的默认结果、查询不存在的数据、手动包装page和自定义SQL,需要的可以参考一下
    2022-03-03
  • Java中的CompletableFuture基本用法

    Java中的CompletableFuture基本用法

    这篇文章主要介绍了Java中的CompletableFuture基本用法,CompletableFuture是java.util.concurrent库在java 8中新增的主要工具,同传统的Future相比,其支持流式计算、函数式编程、完成通知、自定义异常处理等很多新的特性,需要的朋友可以参考下
    2024-01-01
  • Java中的static关键字修饰属性和方法(推荐)

    Java中的static关键字修饰属性和方法(推荐)

    这篇文章主要介绍了Java中的static关键字修饰属性和方法,包括哪些成员属性可以被static修饰,静态属性的访问方法示例详解,需要的朋友可以参考下
    2022-04-04
  • java递归算法的实例详解

    java递归算法的实例详解

    在本篇文章里小编给大家整理了关于java递归算法的实例内容,以及相关知识点总结,需要的朋友们可以学习下。
    2020-02-02
  • Java面向对象基础之多态性,抽象类和接口

    Java面向对象基础之多态性,抽象类和接口

    这篇文章主要介绍了Java面向对象基础:多态性,抽象类和接口,文中代码可以帮助各位更好的理解学习,有需求的小伙伴可以参考下
    2020-05-05

最新评论