第二章 表、栈、队列-LinkedList的数据结构

上一节讲了ArrayList是以数组形式存储的列表,这一节我们讲LinkedList。说到栈和队列,实际上都是表结构。栈和队列只不过是只能从指定地点插入和移除的表。ArrayList也能封装成栈或者队列用途。但是我们有更好的选择,那就是LinkedList。

LinkedList也是有两种构造 

public LinkedList() {
}

/**
 * Constructs a list containing the elements of the specified
 * collection, in the order they are returned by the collection's
 * iterator.
 *
 * @param  c the collection whose elements are to be placed into this list
 * @throws NullPointerException if the specified collection is null
 */
public LinkedList(Collection<? extends E> c) {
    this();
    addAll(c);
}

它实现了List和Deque接口。

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

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

LinkedList是以Node为节点的双向链表结构。first记录初始节点,last基础终节点,size记录长度。

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

Node节点则记录了前一个节点和后一个节点的信息,还有改节点储存的数据 item。所以这是一个知道开始开始节点和末端节点,而且每个节点都知道自己前面是谁、后面是谁。这就跟排队军训的时候报数是一样的。只要你知道你前面是谁和后面是谁,那么不管是从左到右报数还是从右到左报数,就都不会出错。

那么接下来我们讲讲LinkedList的使用

    1.当做列表使用时和ArrayList差不多

       .add

        public boolean add(E e) { linkLast(e); return true; }

   void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
   }

在尾部添加节点的时候简单,就是新建一个Node节点,将之前的末端节点设置为他的prev节点,新添加的节点自然为末端节点,所以next=null;尾部添加节点的时间复杂度较小为O(1)

    .remove

   public boolean remove(Object o) {
    if (o == null) {
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
   }

 

   E unlink(Node<E> x) {
    // assert x != null;
    final E element = x.item;
    final Node<E> next = x.next;
    final Node<E> prev = x.prev;

    if (prev == null) {
        first = next;
    } else {
        prev.next = next;
        x.prev = null;
    }

    if (next == null) {
        last = prev;
    } else {
        next.prev = prev;
        x.next = null;
    }

    x.item = null;
    size--;
    modCount++;
    return element;
}

移除的时间复杂度为O(N)

    2.当做队列使用时

       

        .offer添加到尾部,链表为空时不会报异常

public boolean offer(E e) {
    return add(e);
}
public boolean add(E e) {
    linkLast(e);
    return true;
}
void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

从代码可以知道时间复杂度为O(1)

        .poll返回第一个并删除,没有则返回空,但是不会报错 

public E poll() {
    final Node<E> f = first;
    return (f == null) ? null : unlinkFirst(f);
}
private E unlinkFirst(Node<E> f) {
    // assert f == first && f != null;
    final E element = f.item;
    final Node<E> next = f.next;
    f.item = null;
    f.next = null; // help GC
    first = next;
    if (next == null)
        last = null;
    else
        next.prev = null;
    size--;
    modCount++;
    return element;
}

由代码可以时间复杂度为O(1)

        通过offer和poll来实现先进先出的队列功能,而且时间复杂度非常低为O(1),所以LinkedList非常适合用来做队列。大家可以自己推算一下,如果用ArrayList来做,则时间复杂程度会上升到Q(N)

    3.当做栈使用的时候

        

        .pop删除并返回(弹出)第一个,没有就报错

public E pop() {
    return removeFirst();
}
public E removeFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return unlinkFirst(f);
}

        .push添加到第一个

public void push(E e) {
    addFirst(e);
}
public void addFirst(E e) {
    linkFirst(e);
}
private void linkFirst(E e) {
    final Node<E> f = first;
    final Node<E> newNode = new Node<>(null, e, f);
    first = newNode;
    if (f == null)
        last = newNode;
    else
        f.prev = newNode;
    size++;
    modCount++;
}
 

        .peek返回第一个但是不删除,一般用来检查栈是否为空栈

public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; }

        通过pop和posh来实现先进后出的栈功能。pop函数在栈为空的时候为报错,所以在pop之前需要用peek来判断栈是否为空。

LinkedList也非常适合用来做栈,他的时间复杂程度是O(1),如果用ArrayList来做,时间复杂度会上升为O(N)。

总结:LinkedList的数据结构非常适合用来做栈和队列,因为他的移除和添加末端非常的方便。但是LinkedList不方便用于需要展示任意项的列表模式,因为他的时间复杂度是O(N),而ArrayList则比较适合用展示列表数据,因为他是连续存储的数组形式,访问任意项时间复杂度都是O(1)。

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闽农qq:994955138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值