Java LinkedList特点和底层实现

Java LinkedList特点:

LinkedList底层用双向链表实现的存储。特点:查询效率低,增删效率高,线程不安全。

存储结构图:

在这里插入图片描述

查看源码:
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    transient int size = 0;

    /**
     * Pointer to first node.
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     */
    transient Node<E> last;
自己动手实现下吧:
Step1:构造主框架
package com.ysl.mycollection;

import java.util.LinkedList;

public class SxLinkedList<E> {

    private Node first;
    private Node last;

    private int size;


    public void add(E element){
       
    }

    public void add(E element,int index){
       
    }

    public E get(int index){

    }

    public void remove(int index){
      

    }

    private Node getNode(int index){
        
    }


    public void checkRange(int index){
       
    }
    
    @Override
    public String toString() {
       
    }

    public static void main(String[] args) {
        SxLinkedList<String> list = new SxLinkedList<>();
    
    }
}

Step2:构造节点类
package com.ysl.mycollection;

public class Node {

    Node previous;
    Node next;
    Object element;


    public Node(Node previous, Node next, Object element) {
        this.previous = previous;
        this.next = next;
        this.element = element;
    }


    public Node(Object element) {
        this.element = element;
    }
}

Step3:实现具体方法
	public void add(E element){
        Node node =new Node(element);
        if(first==null){
            first=node;
            last=node;

        }else {
            node.previous=last;
            node.next=null;
            last.next=node;
            last=node;
        }
        size++;
    }

    public void add(E element,int index){
        checkRange(index);
        Node newNode = new Node(element);
        Node temp = getNode(index);
        if(temp!=null){
            Node previous = temp.previous;
            if(previous!=null) {
                previous.next = newNode;
                newNode.previous = previous;
                newNode.next = temp;
                temp.previous = newNode;
            }else {
                first=newNode;
                first.next=temp;
                temp.previous=first;
            }

            size++;
        }
    }

    public E get(int index){

        checkRange(index);
        Node node=getNode(index);

        return node!=null?(E)node.element:null;
    }

    public void remove(int index){
        checkRange(index);
        Node node=getNode(index);
        if(node!=null){
            Node previous = node.previous;
            Node next = node.next;
            if(previous!=null){
                previous.next=next;
            }
            if(next!=null){
                next.previous=previous;
            }
            if(index==0){
                first=next;
            }
            if(index==size-1){
                last=previous;
            }
        size--;
        }

    }

    private Node getNode(int index){
        checkRange(index);
        Node node=null;
        if(index<=(size>>1)){
            node=first;
            for(int i=0;i<index;i++){
                node=node.next;
            }
        }
        else{
            node=last;
            for(int i=size-1;i>index;i--){
                node=node.previous;
            }
        }
        return node;
    }


    public void checkRange(int index){
        if(index<0||index>size-1){
            throw new RuntimeException("索引错误:"+index);
        }
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");

        Node node=first;
        while (node!=null){
            sb.append(node.element+",");
            node=node.next;
        }

        sb.setCharAt(sb.length()-1,']');

        return sb.toString();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值