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的区别

相关推荐
星轨初途3 分钟前
C++入门(算法竞赛类)
c++·经验分享·笔记·算法
没书读了1 小时前
数据结构-考前记忆清单
数据结构
灰灰勇闯IT1 小时前
KMP算法在鸿蒙系统中的应用:从字符串匹配到高效系统级开发(附实战代码)
算法·华为·harmonyos
小龙报1 小时前
【算法通关指南:数据结构和算法篇 】队列相关算法题:3.海港
数据结构·c++·算法·贪心算法·创业创新·学习方法·visual studio
csuzhucong1 小时前
一阶魔方、一阶金字塔魔方、一阶五魔方
算法
五花就是菜1 小时前
P12906 [NERC 2020] Guide 题解
算法·深度优先·图论
辞旧 lekkk2 小时前
【c++】封装红黑树实现mymap和myset
c++·学习·算法·萌新
星轨初途2 小时前
C++的输入输出(上)(算法竞赛类)
开发语言·c++·经验分享·笔记·算法
n***F8752 小时前
SpringMVC 请求参数接收
前端·javascript·算法
Liangwei Lin2 小时前
洛谷 P1025 [NOIP 2001 提高组] 数的划分
算法