当前位置: 首页 > article >正文

【Java 数据结构】LinkedList 类 和 模拟实现链表

 🔥博客主页🔥:【 坊钰_CSDN博客 】

欢迎各位点赞👍评论✍收藏⭐

目录

1. 什么是 LinkedList ?

2 LinkedList 的使用

2.1 LinkedList 的构造

 2.2 LinkedList 的常用方法

2.3 LinkedList 的遍历

3. 单链表的模拟实现

3.1 基本框架

3.2 头插

3.3 尾插

3.4 在第 pos 位后面插入 val

3.5 打印

3.6 求大小

4. 全部源码

5. 小结


1. 什么是 LinkedList ?

对于存储数据来说,ArrayList 是有缺陷的,ArrayList 动态扩容时可能会有空间的损失,而 LinkedList 的元素存储在特定的节点中,通过引用来联系元素之间的关系,效率较高

LinkedList 也是实现了 List 接口

  •  LinkedList 的底层是使用了双链表
  • LinkedList 适合多次频繁插入和删除的场景

2 LinkedList 的使用

2.1 LinkedList 的构造

LinkedList 有两种构造方法

LinkedList()                            //空构造方法
LinkedList(Collection<? extends E>)     //以链表进行构造(必须为 E 的子类)
public class Test {
    public static void main(String[] args) {
        // 空构造
        LinkedList list1 = new LinkedList();

        // 以链表来构造
        LinkedList list2 = new LinkedList(list1);
    }
    
}

 2.2 LinkedList 的常用方法

LinkedList 的常用方法 和 ArrayList的常用方法 基本一样,有兴趣可以看一下上一篇博客

【Java 数据结构】ArrayList 类 与 模拟实现顺序表-CSDN博客

2.3 LinkedList 的遍历

public class Test {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        // for-each 遍历
        for(Integer x : list)
            System.out.print(x);

        //使用迭代器遍历
        ListIterator<Integer> it = list.listIterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
    }
}

3. 单链表的模拟实现

3.1 基本框架

public class MyLinkedList {

    public static class LinkedNode {
        int value;
        LinkedNode next;

        LinkedNode(int value) {
            this.value = value;
        }
    }
}

3.2 头插

/*
 * 头插
 * */

public void addInsert(int val) {
    LinkedNode node = new LinkedNode(val);
    if (head == null) {
        head = node;
    } else {
        node.next = head;
        head = node;
    }
}

3.3 尾插

/*
 * 尾插
 * */

public void fastInsert(int val) {
    LinkedNode node = new LinkedNode(val);
    if (head == null) {
        head = node;
    } else {
        LinkedNode ret = head;
        while (ret.next != null) {
            ret = ret.next;
        }
        ret.next = node;
    }
}

3.4 在第 pos 位后面插入 val

/*
 * 在第 pos 位后面插入 val
 * */

public void posInsert(int pos,int val) {
    LinkedNode node = new LinkedNode(val);
    if (pos <= 0 || pos > linkSize()) {
        System.out.println("Pos is No !");
        return;
    }
    if (pos == linkSize()) {
        fastInsert(val);
        return;
    }
    int count = pos - 1;
    LinkedNode ret = head;
    while (count != 0) {
        ret = ret.next;
        count--;
    }
    node.next = ret.next;
    ret.next = node;

}

3.5 打印

/*
 * 打印
 * */

public void printList() {
    LinkedNode ret = head;
    while (ret != null) {
        System.out.print(ret.value+" ");
        ret = ret.next;
    }
    System.out.println();
}

3.6 求大小

/*
 * 求大小
 * */

public int linkSize() {
    int count = 0;
    LinkedNode ret = head;
    while (ret != null) {
        count++;
        ret = ret.next;
    }
    return count;
}

4. 全部源码

public class MyLinkedList {

    public static class LinkedNode {
        int value;
        LinkedNode next;

        LinkedNode(int value) {
            this.value = value;
        }
    }

    LinkedNode head;

    /*
     * 打印
     * */
    public void printList() {
        LinkedNode ret = head;
        while (ret != null) {
            System.out.print(ret.value+" ");
            ret = ret.next;
        }
        System.out.println();
    }

    /*
     * 求大小
     * */
    public int linkSize() {
        int count = 0;
        LinkedNode ret = head;
        while (ret != null) {
            count++;
            ret = ret.next;
        }
        return count;
    }


    /*
     * 头插
     * */
    public void addInsert(int val) {
        LinkedNode node = new LinkedNode(val);
        if (head == null) {
            head = node;
        } else {
            node.next = head;
            head = node;
        }
    }

    /*
     * 尾插
     * */
    public void fastInsert(int val) {
        LinkedNode node = new LinkedNode(val);
        if (head == null) {
            head = node;
        } else {
            LinkedNode ret = head;
            while (ret.next != null) {
                ret = ret.next;
            }
            ret.next = node;
        }
    }

    /*
     * 在第 pos 位后面插入 val
     * */
    public void posInsert(int pos,int val) {
        LinkedNode node = new LinkedNode(val);
        if (pos <= 0 || pos > linkSize()) {
            System.out.println("Pos is No !");
            return;
        }
        if (pos == linkSize()) {
            fastInsert(val);
            return;
        }
        int count = pos - 1;
        LinkedNode ret = head;
        while (count != 0) {
            ret = ret.next;
            count--;
        }
        node.next = ret.next;
        ret.next = node;

    }
}

5. 小结

以上就是对 ArrayList 类 和 顺序表 的了解,具体还需宝子们去实践,如果觉得该博客对你有用的话,希望一键三连,点个关注不迷路,谢谢支持  


http://www.kler.cn/a/456028.html

相关文章:

  • html+css+js网页设计 美食 全屏幕轮播美食1个页面
  • 基于监督学习的神经网络控制算法详细介绍和例程
  • 数学建模 绘图 图表 可视化(2)
  • mybatis-plus 代码生成
  • 嵌入式开发 的算法与数据结构
  • 5-Linux 系统中VIM 编辑器的基本使用
  • 跨语言学习之C++ 和 Python 的赋值操作 (等号“=“) 的区别
  • MATLAB用find函数结合all,any函数高效解决问题
  • 数据库MySQL(1)
  • 闲谭Scala(1)--简介
  • Windows下Python+PyCharm的安装步骤及PyCharm的使用
  • C++软件设计模式之享元模式(FlyWeight)
  • 【vue】圆环呼吸灯闪烁效果(模拟扭蛋机出口处灯光)
  • Docker中的MYSQL导入本地SQL语句
  • 不用swipe插件,用<component>组件实现H5的swipe切换
  • 【Halcon】例程讲解:基于形状匹配与OCR的多图像处理(附图像、程序下载链接)
  • 3.若依前端项目拉取、部署、访问
  • StableSR: Exploiting Diffusion Prior for Real-World Image Super-Resolution
  • jpeg文件学习
  • SpringCloudAlibaba实战入门之路由网关Gateway断言(十二)