LinkedList的原理以及使用方式

平时的业务中基本上用的都是ArrayList,很少涉及LinkedList,而且LinkedList中有很多api,如果不知道底层原理,有可能会出现报错问题。

1. 简述

LinkedList实现了List和Deque接口,底层维护了一个双向链表,线程不安全,支持操作头节点和尾节点,所以可以当成或者队列使用, 没有容量限制,属于无界队列,线性时间复杂度,对于查找头节点和尾节点比较快,中间节点需要遍历。

public class LinkedList<E> extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
	/**
     * 节点数量
     */
    transient int size = 0;

    /**
     * 头节点
     */
    transient Node<E> first;

    /**
     * 尾节点
     */
    transient Node<E> last;
    
    /**
     * Node节点数据结构
     */
    private static class Node<E> {
        E item;	//当前节点
        Node<E> next;	//上一个节点
        Node<E> prev;	//下一个节点

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
}    

2. 涉及到的api

2.1 队列中添加数据
api名称作用备注
push(E e)向栈顶添加数据无返回值
add(E e)向栈底添加数据默认返回true,如果使用有界队列,会抛错
addFirst(E e)向栈顶添加数据无返回值
addLast(E e)向栈底添加数据无返回值
offer(E e)向栈底添加数据底层调用的还是add,返回boolean
offerFirst(E e)向栈顶添加数据底层调用的还是addFirst,默认返回true
offerLast(E e)向栈底添加数据底层调用的还是addLast,返回true
addAll(int index,Collection c)在指定下标后添加数据底层调用的还是add,返回boolean,会有数组越界的情况
public void push(E e) {
	addFirst(e);	//添加数据到队首
}

public boolean add(E e) {
	linkLast(e);	//添加数据到队尾
	return true;	//默认返回true
}

public void addFirst(E e) {
    linkFirst(e);	//队首添加数据
}

public void addLast(E e) {
    linkLast(e);	//队尾添加数据
}

public boolean offer(E e) {
	return add(e);	//调用的是add方法
}

public boolean addAll(int index, Collection<? extends E> c) {
	//检查下标是否越界,如果越界会抛出 IndexOutOfBoundsException
    checkPositionIndex(index);
    Object[] a = c.toArray();
    int numNew = a.length;
    if (numNew == 0)
        return false;	//如果添加的集合没有元素,返回false
    ----- 向指定下标后添加元素 -------
    return true;
}
2.2 队列中弹出元素
api名称作用备注
pop()弹出栈顶的元素如果没有元素,返回NoSuchElementException()
poll()弹出栈顶的元素如果没有元素返回null
pollFirst()弹出栈顶的元素和poll()底层原理相同
pollLast()弹出栈底的元素如果没有元素返回null
public E removeFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return unlinkFirst(f);
}

public E pop() {
    return removeFirst();
}

public E poll() {
    final Node<E> f = first;
    return (f == null) ? null : unlinkFirst(f);
}

public E pollFirst() {
    final Node<E> f = first;
    return (f == null) ? null : unlinkFirst(f);
}

public E pollLast() {
    final Node<E> l = last;
    return (l == null) ? null : unlinkLast(l);
}
2.3 获取队列元素
api名称作用备注
get(int index)获取指定下标的元素会有下标越界的错误
peek()获取栈顶的元素如果没有元素返回null
getFirst()获取栈顶的元素如果没有元素会抛错
element()获取栈顶的元素底层调用的是getFirst()
getLast()获取栈底的元素没有元素会抛错
public E get(int index) {
    checkElementIndex(index);	//检查下标是否越界
    return node(index).item;
}

public E peek() {
    final Node<E> f = first;
    return (f == null) ? null : f.item;	//如果首节点是null,返回null
}

public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}

public E element() {
    return getFirst();
}

public E getLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return l.item;
}
2.3 其他常用api
api名称作用备注
size()获取队列大小
isEmpty()判断队列是否为空
contains(Object obj)判断队列是否有指定元素返回true或者false
indexOf(Object obj)返回指定元素第一次出现的下标不存在返回-1
lastIndexOf(Object obj)返回指定元素最后一次出现的下标没有返回-1
clear()清空队列中的元素
set(int index, E e)替换指定下标的元素返回被替换的元素,会有下标越界的情况
3. 简单demo实验
3.1 为了避免出错,常用的api
public static void main(String[] args) {
    //队列从左往右分别是队尾和队首,下标从右往左开始计算
    LinkedList<String> queue = new LinkedList<>();
    
    //栈顶添加数据(不会报错,无返回值)
    queue.push("1");
    //栈底添加数据(不会报错, 默认返回ture)
    queue.add("2");
    
    //弹出栈顶元素(如果栈为空,则返回null)
    String first = queue.poll();
    
    //弹出栈底元素(如果栈为空,则返回null)
    String last = queue.pollLast();
    //查询栈顶的元素(如果栈为空,则返回null)
    String first2 = queue.peekFirst();
    //查询栈底的元素(如果栈为空,则返回null)
    String last2 = queue.peekLast();
}
3.2 所有的api demo
public static void main(String[] args) {
    //队列从左往右分别是队尾和队首,下标从右往左开始计算
    LinkedList<String> queue = new LinkedList<>();
    //队首添加元素
    queue.push("1");        //队列:  1
    //队首添加元素
    queue.push("2");        //队列:  1 2
    //队尾添加元素
    queue.add("3");            //队列:  3 1 2
    //队首添加元素
    queue.addFirst("4");    //队列:  3 1 2 4
    //队尾添加元素
    queue.addLast("5");     //队列:  5 3 1 2 4
    //队尾添加元素
    queue.offer("6");       //队列:  6 5 3 1 2 4
    //队尾添加元素
    queue.offerLast("8");       //队列:  8 6 5 3 1 2 4
    //弹出队首元素
    String s1 = queue.pop();        //s1: 4, 队列: 8 6 5 3 1 2
    //弹出队首元素
    String s2 = queue.poll();       //s2: 2, 队列: 8 6 5 3 1
    //弹出队尾元素
    String s3 = queue.pollLast();   //s3: 8, 队列: 6 5 3 1
    //获取队首元素
    String s4 = queue.peek();       //s4: 1, 队列: 6 5 3 1
    //获取队首元素
    String s5 = queue.element();    //s5: 1, 队列: 6 5 3 1
    //获取队尾元素
    String s6 = queue.getLast();    //s6: 6, 队列: 6 5 3 1
    //获取队列大小
    int size = queue.size();    //size: 4
    //判断队列中是否有5
    boolean c1 = queue.contains("5");   //c1: true
    //判断队列中是否有9
    boolean c2 = queue.contains("9");   //c1: false
    //再往队首添加元素
    queue.push("5");                 //队列: 6 5 3 1 5
    //返回5第一次出现的下标(从右到左)
    int index1 = queue.indexOf("5");    //index1: 0
    //返回5最后一次出现的下标
    int index2 = queue.lastIndexOf("5");//index2: 3
    //替换下标为1的元素为9
    String old = queue.set(3, "9");   //old: 5, 队列: 6 9 3 1 5
    //清空队列
    queue.clear();   //队列为空
    //清空队列后,队列大小为0
    int size2 = queue.size();   //size2: 0
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值