LinkedList

一.模拟实现

java 复制代码
public class MyLinkedList {
    static class ListNode {
        private int val;
        private ListNode prev;//前驱
        private ListNode next;//后继

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

    public ListNode head;//双向链表的头节点
    public ListNode last;//双向链表的尾巴

    //得到链表的长度
    public int size(){
        ListNode cur = head;
        int num = 0;
        while (cur != null){
            cur = cur.next;
            num++;
        }
        return num;
    }
    public void display(){
    ListNode cur = head;
    while(cur != null){
        System.out.print(cur.val+" ");
        cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
    ListNode cur = new ListNode(data);
        if (head == null){
            head = cur;
            last = cur;
        }else {
            cur.next = head;
            head.prev = cur;
            head = cur;
        }
    }

    //尾插法
    public void addLast(int data){
        ListNode cur = new ListNode(data);
        if (head == null){
            head = cur;
            last = cur;
        }else {
            last.next = cur;
            cur.prev = last;
            last = cur;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if (index == 0) {
            addFirst(index);
        } else if (index == size()) {
            addLast(data);
        } else {
            ListNode node = new ListNode(data);
            ListNode sea = searchIndex(index);
            node.next = sea;
            node.prev = sea.prev;
            sea.prev = node;
            node.prev.next = node;
        }
    }

    private ListNode searchIndex(int index) {
            ListNode cur = head;
            if (index < 0 || index >= size()){
                System.out.println("位置不合法");
                return null;
            }
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur;
    }

    private void checkIndex(int index) {

    }


    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode node = head;
        while (node != null){
            if (head.val == key){
                head = head.next;
                if (head != null) {
                    head.prev = null;
                }else {
                    last = null;
                }
                return;
            }else if (node.next == null && last.val == key){
                last = last.prev;
                last.next = null;
                return;
            }else if (node.val == key){
                node.prev.next = node.next;
                node.next.prev = node.prev;
                return;
            }
            node = node.next;
        }
        System.out.println("没有这个");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode node = head;
        while (node != null){
            if (head.val == key){
                head = head.next;
                if (head != null) {
                    head.prev = null;
                }else {
                    last = null;
                }
            }else if (node.next == null && last.val == key){
                last = last.prev;
                last.next = null;
            }else if (node.val == key){
                node.prev.next = node.next;
                node.next.prev = node.prev;
            }
            node = node.next;
        }
    }

    public void clear(){
        head = null;
        last = null;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFirst(1);
        myLinkedList.display();
        myLinkedList.addLast(3);
        myLinkedList.display();
        System.out.println(myLinkedList.size());
        System.out.println(myLinkedList.contains(2));
        System.out.println(myLinkedList.contains(99));
        myLinkedList.addIndex(1, 2);
        myLinkedList.display();
        myLinkedList.remove(2);
        myLinkedList.display();
        myLinkedList.addLast(4);
        myLinkedList.addLast(5);
        myLinkedList.addLast(6);
        myLinkedList.addLast(6);
        myLinkedList.addLast(7);
        myLinkedList.display();
        myLinkedList.remove(1);
        myLinkedList.display();
        myLinkedList.removeAllKey(6);
        myLinkedList.display();
        myLinkedList.clear();
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(1);
        myLinkedList.addFirst(2);
        myLinkedList.display();
        myLinkedList.remove(1);
        myLinkedList.display();
        myLinkedList.addLast(2);
        myLinkedList.display();
        myLinkedList.remove(2);
        myLinkedList.display();
        myLinkedList.addFirst(2);
        myLinkedList.display();
        myLinkedList.remove(2);
        myLinkedList.display();
        myLinkedList.addFirst(3);
        myLinkedList.addFirst(2);
        myLinkedList.addLast(3);
        myLinkedList.display();
        myLinkedList.remove(3);
        myLinkedList.display();
        myLinkedList.removeAllKey(1);
        myLinkedList.display();
        myLinkedList.clear();
        myLinkedList.display();
        System.out.println(1);

    }
}

二.LinkedList的使用

1.LinkedList的构造

java 复制代码
public class MyLinkedList {
    public static void main(String[] args) {
        //构造一个空的LinkedList
        List<Integer>list1 = new LinkedList<>();

        List<String>list2 = new java.util.ArrayList<>();
        list2.add("haha");
        list2.add("hehe");
        //使用ArrayList构造LinkedList
        List<String>list3 = new LinkedList<>(list2);
    }
}

2.LinkedList的其他常用方法

三.ArrayList和LinkedList的区别

相关推荐
EterNity_TiMe_6 分钟前
【论文复现】(CLIP)文本也能和图像配对
python·学习·算法·性能优化·数据分析·clip
机器学习之心16 分钟前
一区北方苍鹰算法优化+创新改进Transformer!NGO-Transformer-LSTM多变量回归预测
算法·lstm·transformer·北方苍鹰算法优化·多变量回归预测·ngo-transformer
yyt_cdeyyds27 分钟前
FIFO和LRU算法实现操作系统中主存管理
算法
daiyang123...1 小时前
测试岗位应该学什么
数据结构
alphaTao1 小时前
LeetCode 每日一题 2024/11/18-2024/11/24
算法·leetcode
kitesxian1 小时前
Leetcode448. 找到所有数组中消失的数字(HOT100)+Leetcode139. 单词拆分(HOT100)
数据结构·算法·leetcode
VertexGeek2 小时前
Rust学习(八):异常处理和宏编程:
学习·算法·rust
石小石Orz2 小时前
Three.js + AI:AI 算法生成 3D 萤火虫飞舞效果~
javascript·人工智能·算法
jiao_mrswang3 小时前
leetcode-18-四数之和
算法·leetcode·职场和发展
qystca3 小时前
洛谷 B3637 最长上升子序列 C语言 记忆化搜索->‘正序‘dp
c语言·开发语言·算法