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

相关推荐
忘梓.1 小时前
解锁动态规划的奥秘:从零到精通的创新思维解析(3)
算法·动态规划
️南城丶北离1 小时前
[数据结构]图——C++描述
数据结构··最小生成树·最短路径·aov网络·aoe网络
✿ ༺ ོIT技术༻1 小时前
C++11:新特性&右值引用&移动语义
linux·数据结构·c++
tinker在coding3 小时前
Coding Caprice - Linked-List 1
算法·leetcode
XH华7 小时前
初识C语言之二维数组(下)
c语言·算法
南宫生8 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
不想当程序猿_8 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
落魄君子8 小时前
GA-BP分类-遗传算法(Genetic Algorithm)和反向传播算法(Backpropagation)
算法·分类·数据挖掘
菜鸡中的奋斗鸡→挣扎鸡8 小时前
滑动窗口 + 算法复习
数据结构·算法
Lenyiin8 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin