Java LinkedList特点和底层实现

Java LinkedList特点:

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

存储结构图:

Java LinkedList特点和底层实现_第1张图片

查看源码:

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();
    }

你可能感兴趣的:(Java)